现在的位置: 首页 > 综合 > 正文

iOS 团队开发-UITabbarController合并多个controllers

2018年04月08日 ⁄ 综合 ⁄ 共 1895字 ⁄ 字号 评论关闭

在一个tabbarController中,该如何整合 来自多个storyboard的controller页面呢?

思考?

为什么会有多个storyboard呢?
多人开发的 工程中,根据不同的功能业务模块,可以使用 多个 storyboard, 每个storyboard完成自己模块的controller或其他视图的编写:
最后写完之后,只需要先找到对应的storyboard,再 在该storyboard中找到你所需的controllers即可:

那么如何整合3个/或多个storyboard的controllers页面到一个tabbar上面呢?

直接上代码,慢慢解释 :

1. 使用代码整合多个SB的VC


首先创建一个类,继承自

UITabBarController

//
//  TabBarController.h
//  Aiyu
//
//  Created by http://blog.csdn.net/yangbingbinga on 14/10/24.
//  Copyright (c) 2014年 http://blog.csdn.net/yangbingbinga. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TabBarController : UITabBarController

@end

2.实现文件:

//
//  TabBarController.m
//  Aiyu
//
//  Created by http://blog.csdn.net/yangbingbinga on 14/10/24.
//  Copyright (c) 2014年 http://blog.csdn.net/yangbingbinga. All rights reserved.
//

#import "TabBarController.h"

@interface TabBarController ()

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *vcs=[NSMutableArray arrayWithCapacity:3];//创建一个数组来保存controller对象
    
    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]];
    
    
    UIViewController *vc1=[main instantiateViewControllerWithIdentifier:@"xiaoenai"];//根据storyboard和controller的storeId找到控制器
    UIViewController *vc2=[main1 instantiateViewControllerWithIdentifier:@"xinqing"];
    UIViewController *vc3=[main2 instantiateViewControllerWithIdentifier:@"miyu"];
    
    [vcs addObject:vc1];
    [vcs addObject:vc2];
    [vcs addObject:vc3];
    
    [self setViewControllers:vcs animated:NO];//用当前的viewController数组替换原本的tabbarControlle的 viewControllers数组
}

@end

当然,使用 这些语句之前,需要给ViewController设置标识符:

见下图:

原理:TabbarController有一个数组,ViewControllers,我们只有把我们所有的ViewController放入这个数组即可按顺序显示,

2.如果只有一个SB,可以使用拖拽完成

把TabbarController拖拽到 子ViewController上,选择 VIewControllers就可以了;

原文出处http://blog.csdn.net/yangbingbinga/article/details/43106235

抱歉!评论已关闭.