假设我在我的iPhone应用程序的一个选项卡视图中有一个UIButton
,我想让它在TabBarController
的选项卡栏中打开不同的选项卡。我会如何编写代码来做到这一点?以编程方式切换到TabBar选项卡视图?
我假设我卸载现有的视图并加载特定的选项卡视图,但我不知道如何精确地编写代码。
假设我在我的iPhone应用程序的一个选项卡视图中有一个UIButton
,我想让它在TabBarController
的选项卡栏中打开不同的选项卡。我会如何编写代码来做到这一点?以编程方式切换到TabBar选项卡视图?
我假设我卸载现有的视图并加载特定的选项卡视图,但我不知道如何精确地编写代码。
试试这个:
self.tabBarController.selectedIndex = 1;
或
[self.tabBarController setSelectedIndex:1];
完美,谢谢。尽管每个人都给出了正确的答案,所有人都赞同! – rottendevice 2011-03-24 16:39:09
请注意,如果索引映射到“更多”视图控制器中的一个选项卡(如果您的选项卡多于五个),这将不起作用。在这种情况下,使用'-setSelectedViewController:'。 – 2012-03-02 15:43:43
我这样做后,它成功地更改了标签,我不能再正常选择标签栏。更新:这是在我以编程方式交换选项卡之前调用popToRootViewController的某种方式。 – 2014-05-13 03:35:41
您可以简单地将UITabBarController上的selectedIndex
属性设置为适当的索引,并且该视图将像用户点击选项卡按钮一样更改。
由于一个小的区别是可以通知用户切换选项卡的委托方法未被调用。 – 2011-05-13 17:58:22
设置'selectedIndex'对我来说也不适用于索引> 4,但'selectedViewController'。 – bugloaf 2012-08-14 16:33:58
注意标签被索引从0开始所以下面的代码片段工作
tabBarController = [[UITabBarController alloc] init];
.
.
.
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:4];
进入到第五个标签在酒吧。
thnx男人,你救我的4至5小时... – Ayaz 2013-08-16 06:50:21
我的意见是selectedIndex
或使用objectAtIndex
不一定是切换选项卡的最佳方式。如果您重新排列标签,硬编码索引选择可能会混淆以前的应用程序行为。
如果您想要切换到视图控制器的对象引用,你可以这样做:
tabBarController.selectedViewController = myViewController
当然,你必须确保,即myViewController
真的是在tabBarController.viewControllers
列表。
对于您可能要移动标签的情况,下面是一些代码。
for (UINavigationController *controller in self.tabBarController.viewControllers) {
if ([[controller.childViewControllers objectAtIndex:0] isKindOfClass:[MyViewController class]]) {
[self.tabBarController setSelectedViewController:controller];
break;
}
}
我试过迪斯科S2的建议,它很接近,但这是结束了为我工作。这是在完成另一个选项卡中的操作后调用的。
for (UINavigationController *controller in self.tabBarController.viewControllers)
{
if ([controller isKindOfClass:[MyViewController class]])
{
[self.tabBarController setSelectedViewController:controller];
break;
}
}
我希望能够以指定标签是由类,而不是指数显示,因为我认为这对于一个强大的解决方案,这是较少依赖于你如何线了IB制成。我没有找到任何迪斯科的或Joped的解决方案的工作,所以我创造了这个方法:
-(void)setTab:(Class)class{
int i = 0;
for (UINavigationController *controller in self.tabBarContontroller.viewControllers){
if ([controller isKindOfClass:class]){
break;
}
i++;
}
self.tabBarContontroller.selectedIndex = i;
}
你这样称呼它:
[self setTab:[YourClass class]];
希望这是有益的人
使用中AppDelegate.m
file:
(void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
NSLog(@"Selected index: %d", tabBarController.selectedIndex);
if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
NSUInteger selectedIndex = tabBarController.selectedIndex;
switch (selectedIndex) {
case 0:
NSLog(@"click me %u",self.tabBarController.selectedIndex);
break;
case 1:
NSLog(@"click me again!! %u",self.tabBarController.selectedIndex);
break;
default:
break;
}
}
我有上面的方法的问题,在我的[问题] [1] [1]:http:// stackoverflow。com/questions/19742416/tabbarcontroller-unload-itself-after-call-modalview – 2013-11-02 18:54:27