Tabbarcontroller以编程方式添加选项卡

时间:2014-06-22 14:46:21

标签: ios uitabbarcontroller

对于我正在构建的应用程序,我将所有视图都放在故事板中,并使用tabbarcontrolller在不同视图之间切换。我有一个视图,我想以编程方式添加到已经存在三个项目的现有标签栏。如何以编程方式添加此选项卡,并将其他选项卡保留在故事板中。

更多:我做了你说的但没有发生任何事情,当我添加它仍然只有三个选项卡而不是添加第四个这里是我对标签栏控制器的代码

@interface TheTabBarController ()

@end

@implementation TheTabBarController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIViewController *viewController = [[CrewsViewController alloc]init];
    NSMutableArray *tempArray = [self.tabBarController.viewControllers mutableCopy];
    [tempArray addObject:viewController];
    self.tabBarController.viewControllers = tempArray;
}

@end

3 个答案:

答案 0 :(得分:3)

您可以通过向标签栏控制器的viewControllers数组添加新控制器来实现此目的。因此,通过适合您如何制作控制器的任何方式实例化新控制器。标签栏控制器的viewControllers属性是不可变数组,因此您需要从此数组创建可变数组,将控制器添加到其中,然后将此数组设置为标签栏控制器的viewControllers阵列。

UIViewController *newController = [[UIViewController alloc] init]; // or however is appropriate to instantiate your controller
NSMutableArray *tempArray = [self.tabBarController.viewControllers mutableCopy];
[tempArray addObject:newController];
self.tabBarController.viewControllers = tempArray;

答案 1 :(得分:1)

试试这个:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIViewController *view1 = [[UIViewController alloc] init];
    UIViewController *view2 = [[UIViewController alloc] init];
    UIViewController *view3 = [[UIViewController alloc] init];

    NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
    [tabViewControllers addObject:view1];
    [tabViewControllers addObject:view2];
    [tabViewControllers addObject:view3];

    [self setViewControllers:tabViewControllers];
    //can't set this until after its added to the tab bar
    view1.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view1" 
                                    image:[UIImage imageNamed:@"view1"] 
                                      tag:1];
    view2.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view2" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:2];
    view3.tabBarItem = 
      [[UITabBarItem alloc] initWithTitle:@"view3" 
                                    image:[UIImage imageNamed:@"view3"] 
                                      tag:3];      
}

答案 2 :(得分:0)

swift4:

func viewDidLoad() {

super.viewDidLoad()
let view1 = UIViewController()
let view2 = UIViewController()
let view3 = UIViewController()
var tabViewControllers = [AnyHashable]()
tabViewControllers.append(view1)
tabViewControllers.append(view2)
tabViewControllers.append(view3)
if let aControllers = tabViewControllers as? [UIViewController] {
    viewControllers = aControllers
}

//can't set this until after its added to the tab bar

`view1.tabBarItem = UITabBarItem(title: "view1", image: UIImage(named: "view1"), tag: 1)
view2.tabBarItem = UITabBarItem(title: "view2", image: UIImage(named: "view3"), tag: 2)
view3.tabBarItem = UITabBarItem(title: "view3", image: UIImage(named: "view3"), tag: 3)

}