Ios TabBarController返回null
我在故事板中有一个tabbarcontroller作为初始视图控制器 如何返回nullIos TabBarController返回null,ios,objective-c,null,uitabbarcontroller,Ios,Objective C,Null,Uitabbarcontroller,我在故事板中有一个tabbarcontroller作为初始视图控制器 如何返回null UITabBarController* tabbarController = (UITabBarController*) self.tabBarController; NSLog(@"%@",tabbarController); NSLog(@"%@",self.navigationController.tabBarController); 最初我想做的是 NSMutableArray *newTabs
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];
为什么我会得到null?返回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];
看看乔的答案
如果您有导航控制器,则需要将其添加到您的TabBarViewController.h文件中
@property (nonatomic, retain) UITabBarController * myTabBarController;
然后在viewDidLoad中的TabbarViewController.m文件中,将其分配给self并添加代理
self.myTabBarController = self;
self.myTabBarController.delegate = self;
在Swift中
let indexToRemove = 1
if indexToRemove < (self.viewControllers?.count)! {
var viewControllers = self.viewControllers
viewControllers?.remove(at: indexToRemove)
self.viewControllers = viewControllers
}
让indexToRemove=1
如果indexToRemove<(self.viewControllers?.count)!{
var viewControllers=self.viewControllers
viewControllers?删除(位于:indexToRemove)
self.viewControllers=viewControllers
}
在
UITabBarController
类中直接使用self
,而不是self.tabBarControllers
此代码在哪里?调用的方法是什么?它在ViewDid出现后的XCTabViewController.m
5秒内调用[自执行选择器:@selector(delayed)with object:nil afterDelay:5]那么,self
难道不是UITABBARC控制器吗?你是对的,而不是self.tabBarController
Darn it。如果你发布一条你发现的好消息,我会接受答案。救了我