按钮单击时更改选定的TabBar索引

时间:2012-09-07 07:40:05

标签: ios objective-c iphone uitabbarcontroller

在我的应用程序中,我有4个标签,在第一个标签上:我有4个视图。在第二个视图中,有一个按钮将打开第三个选项卡视图。但我选择的标签索引是一个。我怎么能把它变成第三。

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

  - (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; 

}

4 个答案:

答案 0 :(得分:2)

在应用程序中以编程方式切换标签的最简单方法是执行此操作..

[self.tabBarController setSelectedIndex:3];

我在我的一个应用中使用它,效果非常好。

血浆

答案 1 :(得分:2)

Swift 2中,您只需致电:

    self.tabBarController?.selectedIndex = 0

答案 2 :(得分:1)

按钮单击

[self.navigationController pushViewController:YourView animated:YES];
[self.tabBarController setSelectedIndex:index];

答案 3 :(得分:1)

 self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:3];

试试这个。这对你有帮助。