0
votes

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 to 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.

3

3 Answers

1
votes

It's a bit hard to understand what your intentions with the code are... It seems like you try to do the following:

  1. One of the view controllers accessed through the tab bar is a table view displaying some kind of menu items.
  2. 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.

0
votes

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.

0
votes

for this add navigationController then write

[self.navigationContrller pushViewController:yourClassName animated:YES];