tags:

views:

32

answers:

1

I'm attempting to switch views with an iPhone application- I have a parent view controller, SuperviewController, and then two views that I want to switch within that parent view, MainMenuController and MainGameController.

*EDIT*: I am now using navigation controllers:

SuperviewController.m

viewDidLoad
self.mainMenuController = [[MainMenuController alloc] initWithNibName:@"MainMenu" bundle:nil];
[[self navigationController] pushViewController:self.mainMenuController animated:NO];
switchToMainGame
self.mainGameController = [[MainGameController alloc] initWithNibName:@"MainGame" bundle:nil];
[[self navigationController] pushViewController:self.mainGameController animated:NO];

The app loads correctly with the mainMenu.xib. However, when calling switchToMainGame, nothing happens- it's as if XCode forgot what mainGameController is.

Thanks for any help.

+1  A: 

You might consider swapping view controllers not views, using UINavigationController.

In your AppDelegate.h

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

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

@end

And in -[AppDelegate applicationDidFinishLaunching:] instantiate navigationController, thus:

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

[[self navigationController] setNavigationBarHidden:YES];   

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

Then within SuperviewController.m you can instantiate your MainMenuController and MainGameController, as you already do. To start with MainMenuController you could do this in SuperviewController -viewDidLoad

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

You would need to add some smarts to switch directly between mainMenuController and mainGameController - but it wouldn't be difficult.

So as not to reload nibs again and again, consider defining accessor methods like this:

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

Also, bear in mind that switching between sibling view controllers involve popping current view controller (e.g., mainMenuController) before pushing other view controller (e.g., mainGameController).

westsider
Isn't that what I'm doing- swapping mainMenuController and mainGameController?
Chromium
Your second code snippet makes it look like you are swapping UIViews with -removeFromSuperview and -insertSubview. I am suggesting that you use -pushViewController:animated: and -popViewControllerAnimated:
westsider
It's not a UINavigationController app though... SuperviewController is just a UIViewController, so I don't think I can do that.
Chromium
I'm not convinced that what I am suggesting is the best or the right way to go. However, it is not very hard to add a UINavigationController to your app. I've edited my answer to show some code.
westsider
So would I need to change the SuperviewController to be a UINavigationController?
Chromium
No. UIViewController has a read-only 'navigationController' property, so SuperviewController (which, I assume, subclasses UIViewController) would have that property. You would instantiate that UINavigationController in your AppDelegate.m code, initializing as above and thus associating it with your SuperviewController. At that point the UINavigationController is accessible via your SuperviewController and your other UIViewControllers that are pushed onto navigation stack. It seems strange at first, but it is very nice once you're used to it.
westsider
No, that should be as I typed it. What's happening is you are defining a UINavigationController and consequently the beginning of your navigation stack. And your navigation stack needs at least one UIViewController - which is your root controller: the controller that the user will, typically, see first and which provides the first level of navigation. Each subsequent UIViewController that you push onto the navigation stack will refer to this same UINavigationController - which will allow them to do things like pop themselves or push other UIViewControllers.
westsider
As you can see, I changed my question to more accurately reflect the changes you suggested. However, I'm having the same basic problem- the views are loading correctly, but when I try to change them, nothing happens.
Chromium
Fixed- thanks for the help. I finally found the issue, it was in the calling of the switchViews method itself. (I was calling it, but accidentally instantiated another instance of the SuperviewController instead of calling the method of the particular instance I had going.)
Chromium