如何以编程方式切换到UITabBarController中的另一个选项卡?

Use*_*ser 3 iphone cocoa-touch objective-c ipad ios

我有一个UITableView,我想在UITableView中单击一个单元格时切换到另一个选项卡.

我已经尝试过,但它不起作用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

MainView *mainView = [[MainView alloc] initWithNibName:@"MainView" bundle:nil];
mainView.tabBarController.selectedIndex = 3;  

 }
Run Code Online (Sandbox Code Playgroud)

rob*_*off 15

每次选择行时,您都会加载MainView的新副本.这些新副本不会显示在任何地方.您需要访问现有的主视图实例的标签栏控制器.也许这会奏效:

self.tabBarController.selectedIndex = 3;
Run Code Online (Sandbox Code Playgroud)