【问题标题】:Adding new StoryBoard to UITabbarController将新的 StoryBoard 添加到 UITabbarController
【发布时间】:2015-11-26 18:19:18
【问题描述】:
我的应用中有很多故事板。早些时候我的应用程序不是基于UITabBar 的应用程序,但现在我决定采用更多UITabBar 的方式。现在我想将我的故事板添加到我的UITabBarController。我打开了一个故事板并将其嵌入到 UITabBarController 中,但现在我不知道如何将其他故事板的第一个 UIViewControllers 添加到我的标签栏。我没有任何 tabBarController 属性等。
知道如何将不同的故事板添加到一个UITabBarController
【问题讨论】:
标签:
ios
uitabbarcontroller
uistoryboard
tabbar
【解决方案1】:
如果您不想在一个情节提要中移动所有内容,则应参考标签栏控制器并以编程方式添加每个情节提要的初始视图控制器。
【解决方案2】:
我想我的第一个问题/担心是为什么你有这么多故事板。为什么不将所有视图控制器放在一个故事板上?如果您将所有视图控制器都放在同一个情节提要上,那么将它们连接到 UITabBarController 是一件微不足道的事情 - 从 UITabBarController 控制+拖动一个连接,根据需要尽可能多的视图控制器。
如果出于某种原因需要将视图控制器分布在多个情节提要中,那么您应该能够通过以下方式以编程方式加载它们。在 UITabBarController 的子类中:
- (void)viewDidLoad {
[super viewDidLoad];
UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:@"YourStoryBoardHNameHere1" bundle:nil];
UIViewController *vc1 = [storyboard1 instantiateViewControllerWithIdentifier:@"YourVCNameHere1"];
UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:@"YourStoryBoardHNameHere2" bundle:nil];
UIViewController *vc2 = [storyboard2 instantiateViewControllerWithIdentifier:@"YourVCNameHere2"];
self.viewControllers = @[vc1,vc2];
}
【解决方案3】:
Swift 3 for N8P 的回答:使用多个故事板实现 TabBarController。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tabBar = UITabBarController()
tabBar.viewControllers = self.getArrayOfViewControllers()
self.customiseTabItems(tabBar: tabBar.tabBar)
// set tabBar as root
self.window?.rootViewController = tabBar
self.window?.makeKeyAndVisible()
return true
}
see example