按钮单击时更改选定的TabBar索引 [英] Change the selected TabBar index on button click

查看:79
本文介绍了按钮单击时更改选定的TabBar索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有4个标签,在第一个标签上:我有4个视图。在第二个视图中,有一个按钮将打开第三个选项卡视图。但我选择的标签索引是一个。如何将其变为第三。

In my app I have 4 tabs, on the first Tab : I have 4 views. on second view,there is one button which will open the third tab view. But my selected tab Index is One. How can I chage it to Third.

标签栏的代码在APPDelegate中如下:

My code for tab bar is in APPDelegate as follow:

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
     UINavigationController *nav1 = [[UINavigationController alloc] init];
         UIViewController *viewController1 = [[[CarAccidentAppViewController alloc]
     initWithNibName:@"CarAccidentAppViewController_iPhone" bundle:nil]
     autorelease];
        nav1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];

         //for steps tab...

         UINavigationController *nav2 = [[UINavigationController alloc] init];
         UIViewController *viewController2 = [[[FirstSteps alloc] initWithNibName:@"FirstSteps" bundle:nil] autorelease];
         nav2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];

         //for profiles tab...

        UINavigationController *nav3 = [[UINavigationController alloc] init];
         UIViewController *viewController3 = [[[Profiles alloc] initWithNibName:@"Profiles" bundle:nil] autorelease];
         nav3.viewControllers = [NSArray arrayWithObjects:viewController3, nil];

         //for contact us tab...

        UINavigationController *nav4 = [[UINavigationController alloc] init];
        UIViewController *viewController4 = [[[ContactUs alloc] initWithNibName:@"ContactUs" bundle:nil] autorelease];
         nav4.viewControllers = [NSArray arrayWithObjects:viewController4, nil]; self.tabBarController = [[[UITabBarController alloc] init]
     autorelease];
         self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4 ,nil];

         self.window.rootViewController=self.tabBarController;
        [self.window makeKeyAndVisible];
         return YES; 

}


推荐答案

最简单以编程方式切换应用程序中的选项卡的方法是这样做..

The simplest way to programatically switch tabs within the app is by doing this ..

[self.tabBarController setSelectedIndex:3];

我在我的某个应用程序中使用此功能,效果非常好。

I use this in one of my apps and it works extremely well.

等离子

这篇关于按钮单击时更改选定的TabBar索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆