博客> 用多个StoryBoard构建工程
用多个StoryBoard构建工程
2017-05-26 18:22 评论:2 阅读:1801 zhanglei419723
ios Storyboard Objective-C

我的博客原文地址

一 重新创建Home、Search和About三个StoryBoard

图例1

图例2

图例3

二 创建TabBarViewController类继承于UITabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //第一个 首页
    UINavigationController *homeNav = [[UIStoryboard storyboardWithName:@"Home" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
    UITabBarItem *homeItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
    homeNav.tabBarItem = homeItem;
    homeNav.title = @"首页";

    //第二个 主页
    UINavigationController *searchNav = [[UIStoryboard storyboardWithName:@"Search" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
    UITabBarItem *searchItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
    searchNav.tabBarItem = searchItem;
    searchNav.title = @"搜索";

    //第三个 关于
    UINavigationController *aboutNav = [[UIStoryboard storyboardWithName:@"About" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
    UITabBarItem *aboutItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
    aboutNav.tabBarItem = aboutItem;
    aboutNav.title = @"关于";

    // 将视图控制器添加至数组中
    NSArray *viewControllers = @[homeNav,searchNav,aboutNav];
    self.viewControllers = viewControllers;
}

三 为每个Storyboard添加Storyboard Entry Point

运行工程时发现报错那是因为没有为每个Storyboard添加Storyboard Entry Point,按照下面的步骤操作即可。

图例1

图例2

四 从一个StoryBoard的某一个视图push到另一个StoryBoard的某一个视图

图例6

- (IBAction)storyboardToAbout:(id)sender
{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"About" bundle:nil];
    AboutViewController *vc = [story instantiateViewControllerWithIdentifier:@"VC"];

    [self.navigationController pushViewController:vc animated:YES];
}

最终的效果图 图例7

完整项目代码下载 官方Storyboard例子下载

五 参考

iOS storyboard 添加Entry point

收藏
0
sina weixin mail 回到顶部