-1

在我的应用程序中,我有 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

4 回答 4

3

Swift 2 中,你可以调用:

    self.tabBarController?.selectedIndex = 0
于 2016-06-22T17:21:21.807 回答
2

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

[self.tabBarController setSelectedIndex:3];

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

等离子体

于 2012-09-07T11:06:14.090 回答
1
 self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:3];

尝试这个。这将对您有所帮助。

于 2012-09-07T09:01:02.873 回答
1

点击按钮

[self.navigationController pushViewController:YourView animated:YES];
[self.tabBarController setSelectedIndex:index];
于 2012-09-07T07:42:52.927 回答