如何从一个UIViewController切换到另一个?
我从一个viewcontroller切换到另一个有问题。如何从一个UIViewController切换到另一个?
以下是我有:
在我的 “AppDelegate_iPad.m”:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect frame = [UIScreen mainScreen].bounds;
window = [[UIWindow alloc] initWithFrame:frame];
window.backgroundColor = [UIColor blackColor];
LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:@"LoginViewController_iPad" bundle:nil];
[window addSubview:loginView.view];
[self.window makeKeyAndVisible];
return YES;
}
工程。现在我在该屏幕上有一个登录按钮,当按下它时,它就是这样的:
- (IBAction)doPressLoginButton
{
DebugLog(@"doPressLoginButton");
MenuViewController_iPad *menuView = [[MenuViewController_iPad alloc] initWithNibName:@"MenuViewController_iPad" bundle:nil];
[self.navigationController pushViewController:menuView animated:YES];
[menuView release];
}
问题是什么也没发生。我假设我错过了实际的导航调节器?!
希望,任何人都可以提供帮助。
谢谢
你应该建立在你的应用程序委托一个UINavigationController实例变量。然后,确保将其合成并在dealloc方法中释放它。
你实现application:didFinishLaunchingWithOptions:
看起来是这样的:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // window is usually an IBOutlet CGRect frame = [UIScreen mainScreen].bounds; window = [[UIWindow alloc] initWithFrame:frame]; window.backgroundColor = [UIColor blackColor]; LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:@"LoginViewController_iPad" bundle:nil]; navigationController = [[UINavigationController alloc] initWithRootViewController:loginView]; [loginView release]; [self.window addSubview:navigationController.view]; [self.window makeKeyAndVisible]; return YES; }
现在,你就可以使用self.navigationController。
我认为我缺少实际navigationconroller?!
你是对的。 :d
self.navigationController
返回nil,如果你不建立一个NavigationController。
而且任何消息发送到nil
对象将被忽略。
如果你只需要从一个切换到另一个。使用
[self presentModalViewController:modalViewController animated:YES];
代替。
切换会好,但presentModalViewController会抛出一个错误:请求成员'presentModalViewController'的东西不是结构或联合 – eemceebee 2011-01-08 17:35:44
关于您对有关窗口是IBOutlet的评论。那么我知道,但我没有MainWindow.xib,所以我自己创建窗口。 – eemceebee 2011-01-09 11:29:36