Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 单击按钮更改选定的选项卡栏索引_Ios_Objective C_Iphone_Uitabbarcontroller - Fatal编程技术网

Ios 单击按钮更改选定的选项卡栏索引

Ios 单击按钮更改选定的选项卡栏索引,ios,objective-c,iphone,uitabbarcontroller,Ios,Objective C,Iphone,Uitabbarcontroller,在我的应用程序中,我有4个选项卡,在第一个选项卡上:我有4个视图。在第二个视图中,有一个按钮将打开第三个选项卡视图。但我选择的选项卡索引是一个。我怎样才能把它改到第三位呢 我的选项卡栏代码在APPDelegate中,如下所示: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UINavigationContr

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

}
点击按钮

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

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

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

[self.tabBarController setSelectedIndex:3];
我在我的一个应用程序中使用了它,它工作得非常好

Plasma

在Swift 2中,您只需拨打:

    self.tabBarController?.selectedIndex = 0

这是工作,但我转到第三个选项卡的第一个视图。我想转到第三个选项卡的第二个视图控制器。这在我的应用程序中运行良好,在我意识到Rajneesh071发布了相同的代码片段之前,我回答了这个问题。但是我想查看第三个选项卡的第二个视图只需单击按钮导航到您的第二个视图…并设置tabBarIndex=3..chk我的更新答案但是我想查看第三个选项卡的第二个视图可能还有其他方法,但我会在某个地方设置一个变量,我会在选项卡的viewDidLoad上读到。因此,您将myVC设置为2,然后切换到选项卡3,当它加载时,它将检查您之前设置为2的myVC,并加载第二视图控制器。
    self.tabBarController?.selectedIndex = 0