Problems with switching content

I am trying to switch views using an iPhone app. I have a parent view controller SuperviewController

, and then two views that I want to include in that parent view, MainMenuController

and MainGameController


* EDIT * : I am now using navigation controllers:


self.mainMenuController = [[MainMenuController alloc] initWithNibName:@"MainMenu" bundle:nil];

[[self navigationController] pushViewController:self.mainMenuController animated:NO];

self.mainGameController = [[MainGameController alloc] initWithNibName:@"MainGame" bundle:nil];

[[self navigationController] pushViewController:self.mainGameController animated:NO];

The app loads correctly from mainMenu.xib. However, when switchToMainGame is called , nothing happens - as if Xcode has forgotten what mainGameController is.

Thanks for any help.


source to share

1 answer

You might consider switching non-view view controllers using the UINavigationController.

In AppDelegate.h application

@interface AppDelegate : NSObject <UIApplicationDelegate> 
    UIWindow *window;
    UINavigationController *navigationController;

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;



And in - [AppDelegate applicationDidFinishLaunching:] creates an instance of navigationController, thus:

[self setNavigationController:[[UINavigationController alloc] initWithRootViewController:mySuperviewController]];

[[self navigationController] setNavigationBarHidden:YES];   

//  Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];


Then in SuperviewController.m you can instantiate your MainMenuController and MainGameController as you have already done. To start with MainMenuController you can do it in SuperviewController -viewDidLoad

[[self navigationController] pushViewController:[self mainMenuController] animated:YES];


You will need to add some smarts to switch between mainMenuController and mainGameController, but that shouldn't be difficult.

To avoid reloading nibs over and over, consider defining accessor methods, for example:

- (MainGameController*) mainGameController
if (mainGameController == nil)
    mainGameController = [[MainGameController alloc] initWithNibName:@"MainGame" bundle:nil];
return mainGameController;


Also, keep in mind that when switching between child view controllers, the display controller of the current type (such as mainMenuController) is invoked before clicking another view controller (such as mainGameController).



All Articles