我有基于窗口的应用程序,我添加了MainMenuViewController .h和.m,并在AppDelegate.m中创建了tabBarController
MainMenuViewController *mmvc = [[MainMenuViewController alloc] init];
FavesViewController *fvc = [[FavesViewController alloc] init];
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:mmvc, fvc, nil];
[self.window addSubview:tbc.view];
在MainMenuViewController中,我有一个按钮,其中有一个动作 - 转到其他视图
-(IBAction) goToTableView {
MyViewController *mtvc = [[MyViewController alloc] init];
[self.view addSubview:mvc.view];
}
但是当我按下Main Menu tabBar项时,它会加载MyTableView,而不是MainMenuViewController . 我希望当我按TabBar项MainMenu它将加载MainMenuViewController,而不是从MainMenuViewController加载的ViewController . 怎么做?谢谢 .
3 回答
有点难以理解您对代码的意图是什么......似乎您尝试执行以下操作:
通过选项卡栏访问的其中一个视图控制器是一个显示某种菜单项的表视图 .
点击菜单中的项目应导航到另一个视图控制器 .
如果以上是正确的,那么您的解决方案是设置标签栏项以显示UINavigationController的实例,其中rootViewController属性指向MainMenuViewController类的实例 .
然后在菜单视图控制器的动作处理程序中,您将执行以下操作:
当您想要遍历某种菜单细节层次结构时,经验法则是使用导航控制器 .
你这样做是错的 . 您应该将MainMenuViewController放在UINavigationController中,然后使用
[self.navigationController pushViewController:mtvc animated:YES]
添加它(如果需要以编程方式删除它,则使用[self.navigationController popViewControllerAnimated:YES]
),或者您应该使用[self presentModalViewController:mtvc animated:YES]
添加它和[self dismissModalViewControllerAnimated:YES]
来删除它 .为此添加navigationController然后写
[self.navigationContrller pushViewController:yourClassName animated:YES];