button单击更改选定的TabBar索引

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

我的标签栏的代码是在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; } 

编程切换应用程序内的标签最简单的方法是通过这样做..

 [self.tabBarController setSelectedIndex:3]; 

我在我的一个应用程序中使用它,它工作得非常好。

等离子体

点击button

 [self.navigationController pushViewController:YourView animated:YES]; [self.tabBarController setSelectedIndex:index]; 
  self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:3]; 

尝试这个。 这对你有帮助

Swift 2中,你可以调用:

  self.tabBarController?.selectedIndex = 0