justinpeck
justinpeck

Reputation: 141

Creating a TabBarControllerDelegate in a Storyboard

I'm having difficulty creating a UITabBarControllerDelegate in my Storyboard driven iOS5 application. Here is the situation:

  1. I have an initial screen that will eventually handle login but which currently just has a button that sends the user to...
  2. ...a Tab Bar Controller with five tabs. Each of these tabs go to...
  3. ...a Navigation Controller with many child View Controllers under the root.

(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

Answers (2)

marcos1490
marcos1490

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

Alladinian
Alladinian

Reputation: 35626

There are more than one solutions to your problem (its a matter of design pattern decision). Some of them could be:

  1. 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

  2. Pop to the root by calling -popToRootViewControllerAnimated from the viewWillDisappear event of every view that you want this behavior implemented

Upvotes: 1

Related Questions