问题描述
我正在尝试使用iPhone应用程序切换视图-我有一个父视图控制器 SuperviewController
,然后有两个要在该父视图中切换的视图 MainMenuController
和 MainGameController
.
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 * :我现在正在使用导航控制器:
*EDIT*: I am now using navigation controllers:
viewDidLoad self.mainMenuController = [[[MainMenuController alloc] initWithNibName:@"MainMenu" bundle:nil];
[[[self navigationController] pushViewController:self.mainMenuController动画:否];
switchToMainGame self.mainGameController = [[[MainGameController alloc] initWithNibName:@"MainGame" bundle:nil];
[[[self navigationController] pushViewController:self.mainGameController animation:NO];
应用程序通过mainMenu.xib正确加载.但是,当调用 switchToMainGame 时,什么也没发生-好像XCode忘记了mainGameController是什么.
The app loads correctly with the mainMenu.xib. However, when calling switchToMainGame, nothing happens- it's as if XCode forgot what mainGameController is.
感谢您的帮助.
推荐答案
您可能考虑使用UINavigationController交换视图控制器 not 视图.
You might consider swapping view controllers not views, using UINavigationController.
在您的AppDelegate.h
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
在-[AppDelegate applicationDidFinishLaunching:]中实例化navigationController,因此:
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];
然后在SuperviewController.m中,您可以像实例一样实例化MainMenuController和MainGameController.要从MainMenuController开始,您可以在SuperviewController -viewDidLoad
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];
您需要添加一些技巧以直接在mainMenuController和mainGameController之间进行切换-但这并不困难.
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;
}
此外,请记住,在同级视图控制器之间进行切换涉及在推入其他视图控制器(例如mainGameController)之前弹出当前视图控制器(例如mainMenuController).
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).
这篇关于切换内容视图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!