Creating a TabBarControllerDelegate in a storyboard

It's hard for me to create a UITabBarControllerDelegate in my iOS5 storyboard based application. Here is the situation:

  • I have an initial screen that will eventually process the login, but currently it has a button that sends the user ...
  • ... a five-tab tab panel controller. Each of these tabs goes ...
  • ... a navigation controller with many child view controllers under the root.

(If this helps, a screenshot of the corresponding section of the storyboard 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 last one to visit the View Controller (which is the default behavior).

I understand that for this I need to call popToRootViewControllerAnimated when Tab is pressed, as discussed here and here , but I cannot figure out how to do this in the storyboard. How can I do this without giving up the storyboard and getting started?

Thanks!

+4
source share
2 answers

There are several solutions to your problem (this is a question of solving a design template). Some of them may be:

  • Subclass UITabBarController and set it as the custom class of your tab in the storyboard (also connect the delegate to your object for processing) and override the -tabBarController: didSelectViewController: delegate method

  • Get to the root by calling -popToRootViewControllerAnimated from the viewWillDisappear event of each view that you want to implement.

+1
source

You can create your own TabBarController, implement a method that creates 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 an array with view controllers, which in your case will be the NavigationController identifier that you set in 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]; } 
+1
source

Source: https://habr.com/ru/post/1389401/


All Articles