在一个tabbarController中,该如何整合 来自多个storyboard的controller页面呢?
为什么会有多个storyboard呢?多人开发的工程中,根据不同的功能业务模块,可以使用多个storyboard,每个storyboard完成自己模块的controller或其他视图的编写:最后写完之后,只需要先找到对应的storyboard,然后在该storyboard中找到你所需的controllers即可。
那么如何整合3个或多个storyboard的controllers页面到一个tabbar上面呢?
直接上代码,慢慢解释
#import TabBarController.h
@interface TabBarController ()
@end
@implementation TabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建一个数组来保存controller对象
NSMutableArray *vcs = [NSMutableArray arrayWithCapacity:3];
UIStoryboard *main = [UIStoryboard storyboardWithName:@Main bundle:[NSBundle mainBundle]]; // 首先找到对应的storyboard
UIStoryboard *main1 = [UIStoryboard storyboardWithName:@Main1 bundle:[NSBundle mainBundle]];
UIStoryboard *main2 = [UIStoryboard storyboardWithName:@Main2 bundle:[NSBundle mainBundle]];
// 根据storyboard和controller的storyboardID找到控制器
UIViewController *vc1 = [main instantiateViewControllerWithIdentifier:@"one"];
UIViewController *vc2 = [main1 instantiateViewControllerWithIdentifier:@"two"];
UIViewController *vc3 = [main2 instantiateViewControllerWithIdentifier:@"three"];
[vcs addObject:vc1];
[vcs addObject:vc2];
[vcs addObject:vc3];
// 用当前的viewController数组替换原本的tabbarControlle的viewControllers数组
[self setViewControllers:vcs animated:NO];
}
当然,使用 这些语句之前,需要给ViewController设置标识符: