Reputation: 141
I'm having difficulty creating a UITabBarControllerDelegate in my Storyboard driven iOS5 application. Here is the situation:
(If it helps, a screenshot of the relevant Storyboard section is here.)
When the user switches tabs, I always want the user to be directed to the Root View Controller for that particular Navigation Controller, and not the most recently visited View Controller (which is the default behavior).
I understand that to do so, I need to call popToRootViewControllerAnimated when a Tab is pressed as discussed here and here, but I can't figure out how to do that within the storyboard. How can I do this without scrapping the storyboard and starting over?
Thanks!
Upvotes: 4
Views: 1387
Reputation: 362
You can create your own TabBarController, implement a method that instantiate your view controllers
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title
viewController(NSString *)viewController {
UIViewController* returnViewController = [self.storyboard
instantiateViewControllerWithIdentifier:viewController];
return returnViewController;
}
Then in the viewDidLoad method you create the array with the view controllers, that in your case would be the NavigationController's identifier that you set on the InterfaceBuilder.
- (void)viewDidLoad {
self.viewControllers=
[NSArray arrayWithObjects:
[self viewControllerWithTabTitle:@"Option 1" viewController:@"viewController1"],
[self viewControllerWithTabTitle:@"Option 2" viewController:@"viewController2"],
[self viewControllerWithTabTitle:@"Option 3" viewController:@"viewController3"],
[self viewControllerWithTabTitle:@"Option 4" viewController:@"viewController4"],
[self viewControllerWithTabTitle:@"Option 5" viewController:@"viewController5"], nil];
}
Upvotes: 1
Reputation: 35626
There are more than one solutions to your problem (its a matter of design pattern decision). Some of them could be:
Subclass UITabBarController and set it as the custom class of your tabbar in your storyboard (also connect the delegate to your object in order to be handled) and override the -tabBarController:didSelectViewController: delegate method
Pop to the root by calling -popToRootViewControllerAnimated from the viewWillDisappear event of every view that you want this behavior implemented
Upvotes: 1