在viewDidLoad中的tabBarController上设置SelectedIndex不起作用

时间:2013-09-30 10:05:15

标签: ios objective-c

我在尝试设置tabBarController的当前选项卡时遇到问题,其中我有两个嵌套的视图。应用程序启动时,我想更改正在显示的默认选项卡。因此,我在其中一个嵌套视图控制器的viewDidLoad中执行此操作。我可以通过以下方式禁用tabBarController:

 self.tabBarController.tabBar.userInteractionEnabled = NO;

并且tabBarController有一个地址(它存在)。

我尝试了一些更改标签的不同变体,包括:

self.tabBarController.selectedIndex = 1;

[self.tabBarController setSelectedIndex:1];
然而,似乎都不起作用。但是,当我通过IB动作调用这些函数时,它们可以工作。

这是由一个嵌套控制器完成的。也许这必须在其他地方完成?

2 个答案:

答案 0 :(得分:1)

在UITabBarController上使用selectedIndex属性。

 controller.selectedIndex = tabBarButtonIndex;

或使用

[controller setSelectedIndex:tabBarButtonIndex];

希望这会有所帮助

答案 1 :(得分:0)

确保您先拨打[super viewDidLoad]

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tabBarController setSelectedIndex:1];
}
相关问题