简体   繁体   中英

Proper Way to Open a New Storyboard through UITabBarController

We are working on splitting our main storyboard into smaller ones so that it makes source control merging easier. Any ideas on what the right approach is to load a new storyboard from a UITabBar?

Here's what we have so far in our custom subclassed UITabBarController:

UITabBarItem *cardsTabItem = [self.tabBar.items objectAtIndex:kTabBarIndexCards];

    cardsTabItem.image = [[UIImage imageNamed:@"navCardsOff"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    cardsTabItem.selectedImage = [[UIImage imageNamed:@"navCardsOn"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    cardsTabItem.imageInsets = UIEdgeInsetsMake(-5, 0, 5, 0);
    cardsTabItem.titlePositionAdjustment = UIOffsetMake(0, -5);

I've done the same thing before, but with a UITabBarController. In that case we had a storyboard for each of the tab buttons, even though one of the storyboards only had 1 view controller in it. I think wether you're using a UITabBarController or responding to the tab bar delegate the answer is the same. For each button clicked make the determination of which storyboard the view controller you want to load is in:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"leftButtonStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *vc = [storyboard instantiateInitialViewController];

//or

UIStoryboard *otherVC = [storyboard instantiateViewControllerWithIdentifier:@"CameraViewController"];

Then you can present it, push it, or whatever.

In my case since I was using a UITabBarController this was all done during initialization of the controller for all the different buttons.

It will most likely come in handy to by default name all of the different view controller your using in your storyboard (the storyboard id), I usually name them after the viewController class so I don't have to remember what I called it.

I would also recommend that you avoid using the self.storyboard property when trying to instantiate another view controller because you might end up with a situation where a controller is shared between tabs. Being explicit with which storyboard you're loading a controller from can help with readability and avoidance of bugs.

Edit - a more concrete example:

What you need to do is set the viewControllers property of your UITabViewController, I do this in its init method. For example

- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *one = [mainStoryboard instantiateViewControllerWithIdentifier:@"VC1"];
    UIViewController *two = [mainStoryboard instantiateViewControllerWithIdentifier:@"VC2"];

    self.viewControllers = @[one,two];
    }
return self;
}

You can use this technique if your writing it in code itself or if you're using a storyboard. Beware that if you have other view controllers already hooked up via the storyboard you'll loose then unless you instantiate them there as well. You can also use the setViewControllers: animated: method as well.

The code for creating the custom tab bar items (the buttons at the bottom) should probably go within the individual view controllers and be assigned to its tabBarItem property. The UITabBarController will use that property to create the correctly styled button. If you don't provide the property you get the default buttons starting from 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM