self.navigationController.tabBarController.selectedIndex不起作用
我有一个UITabBarController
包含4个不同的UIViewControllers
。
在第一个选项卡上有一个包含其子UIViewController
的UINavigationViewController
。 而不点击标签栏,我想要在第二个选项卡上的用户。 为此我尝试了:
self.navigationController.tabBarController.selectedIndex = 1;
但它不工作。
忽略任何错误,我是一个新手。
谢谢。
在尝试更改选项卡之前执行以下操作:
UITabBarController *tab=self.tabBarController; if (tab){ NSLog(@"I have a tab bar"); [self.tabBarController setSelectedIndex:1]; } else{ NSLog(@"I don't have"); }
你试一试
self.tabBarController.selectedIndex = 1
尝试将您的制表符切换代码移动到viewDidLoad
我validation了这可以在xcode 4中内置的选项卡式应用程序模板项目中工作
- (void)viewDidLoad { [super viewDidLoad]; self.tabBarController.selectedIndex = 1; }
我也看看你的AppDelegate类的东西沿线的东西
self.tabBarController = [[[UITabBarController alloc] init] autorelease]; self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; self.window.rootViewController = self.tabBarController;
如果你没有看到这些行,那么你会想find你的代码中的UITabBarViewController正在被初始化,而不是使用该对象指针。
调用tabbarcontroller.selectedindex
你必须find从导航控制器的tabbarcontroller,如果你不使用push on navigationController只是使用navigationController的topViewController属性来获取您的tabBarController的实例,并将其分配给UITabBarController和我们selectedIndex,这将工作正常
如果设置委托试试这个:
self.selectedIndex = 1;
你是tabBarController 🙂
我们应该把后面的部分放在viewDidAppear中。
- (void)viewDidAppear:(BOOL)animated { NSArray *ary = [self.tabBarController viewControllers]; UIViewController *vc = ary[3]; [self.tabBarController setSelectedIndex:3]; [self.tabBarController setSelectedViewController:vc]; }
如果您正在呈现一个新的控制器(与当前屏幕上的控制器不同),则应该在该function的完成块中设置索引以呈现您的视图控制器。 如果您在提交之前将其设置,则可能无法正常工作。
在我的情况下,我已经embedded导航控制器内的选项卡控制器。 我提出了新的导航控制器newNavContoller
,一旦提交,我在我的标签控制器tvc
设置索引:
currentViewController?.presentViewController(newNavController, animated: true, completion: {tvc.selectedIndex = selectedIndex})