Question

I have a project that is a "tab-bar controller" app. The first button is essentially a Home screen. The second one displays a UITableView of content. The third button displays a different UITableView, etc.

From the first view (Home), I have a button on the page that is functionally equivalent to the second-button of the tab controller. It is an alternative path to get to that UITableView. I do not want to send a button press to the AppDelegate's UITabBarController.

The code in HomeViewController that I want is essentially this:

-(IBAction) touchInsideButton:(id)sender {
    [self presentModalViewController: [appDelegate secondViewController] animated:YES];
}

or

-(IBAction) touchInsideButton:(id)sender {
    AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
    [appDelegate launchSecondViewController: self];
}

where, "launchSecondViewController" is (uncomment-out one of the two lines)

-(void) launchSecondViewController: (id) sender {
//    [self.tabBarController presentModalViewController: secondViewController animated:YES];
//    [self.tabBarController.navigationController pushViewController: secondViewController animated:YES];
}

Regardless, all three approaches give the same error:

2013-05-16 12:04:26.977 MyApp[55273:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller .' * First throw call stack:

Was it helpful?

Solution

The problem is that this instance of the second view controller, which you refer to as secondViewController, is already in the interface, as a child of the tab view controller. You cannot have it be there and also present or push it.

The way to manipulate a tab bar controller in code is to set is selectedViewController (or selected index). Try doing that instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top