【问题标题】:TabBarController returns nullTabBarController 返回 null
【发布时间】:2015-10-12 19:15:16
【问题描述】:

我在故事板中有一个 tabbarcontroller 作为初始视图控制器

这个怎么返回null

UITabBarController* tabbarController = (UITabBarController*) self.tabBarController;
NSLog(@"%@",tabbarController);
NSLog(@"%@",self.navigationController.tabBarController);

原来我想做的事

NSMutableArray *newTabs = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
NSLog(@"\n\n\n%lu\n\n\n",(unsigned long)[newTabs count]);
NSLog(@"self.tabBarController.viewControllers %@ \n\n",self.tabBarController);
[newTabs removeObjectAtIndex: 1];
[self.tabBarController setViewControllers:newTabs];

为什么我会得到空值?

【问题讨论】:

  • 这段代码在哪里?在什么方法中被调用?
  • 在viewdidappear @987654328XCTabViewController.m 5秒后调用
  • 那么,self 不是 UITabBarController 吗?而不是self.tabBarController
  • 该死,你是对的。如果您发布一个,将接受答案

标签: ios objective-c null uitabbarcontroller


【解决方案1】:

返回null的原因是self是你的UITabBarController,因此self.tabBarController上没有任何内容

您的代码应如下所示:

NSMutableArray *newTabs = [NSMutableArray arrayWithArray:[self viewControllers]];
NSLog(@"\n\n\n%lu\n\n\n",(unsigned long)[newTabs count]);
NSLog(@"self.viewControllers %@ \n\n",self);
[newTabs removeObjectAtIndex: 1];
[self setViewControllers:newTabs];

【讨论】:

  • 很好,你发现了。救了我
【解决方案2】:

斯威夫特

        let indexToRemove = 1
        if indexToRemove < (self.viewControllers?.count)! {
            var viewControllers = self.viewControllers
            viewControllers?.remove(at: indexToRemove)
            self.viewControllers = viewControllers
        }

UITabBarController 类中直接使用self,而不是self.tabBarControllers

【讨论】:

    【解决方案3】:

    查看乔的回答self.tabBarController is NULL

    如果您有导航控制器,则需要将其添加到 YourTabBarViewController.h 文件中

     @property (nonatomic, retain) UITabBarController * myTabBarController;
    

    然后在 viewDidLoad 的 YourTabBarViewController.m 文件中将其分配给 self 并添加委托

    self.myTabBarController = self;
    self.myTabBarController.delegate = self;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2020-06-19
      • 2011-10-15
      相关资源
      最近更新 更多