I have window-based application, I add MainMenuViewController .h and .m, and in AppDelegate.m I created 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];
In MainMenuViewController I have button which action is - go t开发者_开发技巧o other View
-(IBAction) goToTableView { MyViewController *mtvc = [[MyViewController alloc] init]; [self.view addSubview:mvc.view]; }
But when I press in Main Menu tabBar item it loads MyTableView, not MainMenuViewController. I want that when I press tabBar item MainMenu it will loading MainMenuViewController, not ViewController that was loading from MainMenuViewController. How to do that? Thank you.
It's a bit hard to understand what your intentions with the code are... It seems like you try to do the following:
- One of the view controllers accessed through the tab bar is a table view displaying some kind of menu items.
- Tapping on an item in the menu should navigate to another view controller.
If the above is correct, then your solution would be to set the tab bar item to display an instance of UINavigationController, which rootViewController property points to an instance of the MainMenuViewController class.
Then in the action handler of the menu view controller you'd do something like this:
MyViewController *mtvc = [[MyViewController alloc] init];
[self.navigationController pushViewController:mtvc animated:YES];
[mtvc release], mtvc = nil;
The rule of thumb is to use a navigation controller, when you want to traverse some kind of a menu-details hierarchy.
You're doing it wrong. You should either put MainMenuViewController inside a UINavigationController and then use [self.navigationController pushViewController:mtvc animated:YES]
to add it (and [self.navigationController popViewControllerAnimated:YES]
if you need to programmatically remove it), or you should use [self presentModalViewController:mtvc animated:YES]
to add it and [self dismissModalViewControllerAnimated:YES]
to remove it.
for this add navigationController then write
[self.navigationContrller pushViewController:yourClassName animated:YES];
精彩评论