开发者

Switching Content Views Issue

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 XCod开发者_JAVA百科e forgot what mainGameController is.

Thanks for any help.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜