简体   繁体   English

通过UITabBarController打开新故事板的正确方法

[英]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? 关于从UITabBar加载新的故事板的正确方法的任何想法?

Here's what we have so far in our custom subclassed UITabBarController: 到目前为止,这是我们自定义子类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. 我之前做过同样的事情,但是使用了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. 我认为无论您使用的是UITabBarController还是响应选项卡栏委托,答案都是相同的。 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. 就我而言,由于我使用的是UITabBarController,因此所有这些操作都是在控制器初始化期间为所有不同的按钮完成的。

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. 默认情况下,它很可能会方便地命名您在情节提要中使用的所有不同视图控制器(情节提要ID),我通常以viewController类命名它们,因此我不必记住我叫它的名字。

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. 我还建议您在尝试实例化另一个视图控制器时避免使用self.storyboard属性,因为您可能最终会遇到在选项卡之间共享控制器的情况。 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. 您需要做的是设置UITabViewController的viewControllers属性,我可以在其init方法中进行此操作。 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. 您也可以使用setViewControllers:animation:方法。

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. 用于创建自定义标签栏项目的代码(底部的按钮)可能应该放在各个视图控制器中,并分配给其tabBarItem属性。 The UITabBarController will use that property to create the correctly styled button. UITabBarController将使用该属性来创建样式正确的按钮。 If you don't provide the property you get the default buttons starting from 1. 如果您不提供该属性,则会从1开始获得默认按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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