Err: "App tried to present modally an active controller"


#1

When generating the imagePicker controller, we presented this view with the DetailViewController.

When presenting the new item’s detailViewController through ItemViewController, why must we first create a new NavigationController and present the navigation controller instead?


#2

If you comment out the code as shown below, and change the last statement (presentViewController) to be the detailViewController, then run the app and tap on the + button, you will get the answer.

[code]- (void)addNewItem:(id)sender
{
BNRItem *newItem = [[BNRItemStore sharedStore] createItem];

DetailViewController *detailViewController = [[DetailViewController alloc] initForNewItem:YES];

[detailViewController setItem:newItem];

[detailViewController setDismissBlock:^{
    [[self tableView] reloadData];
}];

//UINavigationController *navController = [[UINavigationController alloc] 
//                                         initWithRootViewController:detailViewController];

//[navController setModalPresentationStyle:UIModalPresentationFormSheet];
//[navController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

[self presentViewController:detailViewController animated:YES completion:nil];

}[/code]

So, to get our navigation controller to provide us with the UINavigationBar with our buttons, we need to create an instance of UINavigationController.

We then set its rootViewController as detailViewController, and by doing so, our parent view controller is now navController, so this is what we need to send as the argument of presentViewController.

I hope this helps,
Nick
http://myfirstiphoneapp.co.uk