如何从一个UIViewController切换到另一个?

时间:2011-01-08 17:14:50

标签: iphone ipad uiviewcontroller

从一个viewcontroller切换到另一个viewcontroller时遇到问题。

这就是我所拥有的:

在我的“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];

}

问题在于没有任何事情发生。我假设我错过了实际的navigationconroller?!

希望任何人都可以提供帮助。

谢谢

2 个答案:

答案 0 :(得分:1)

您应该在app delegate中创建一个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。

答案 1 :(得分:0)

  

我认为我错过了实际的navigationconroller?

你是对的。 :d

如果您没有设置NavigationController,

self.navigationController将返回nil 发送给nil对象的任何消息都将被忽略。

如果您只需要从一个切换到另一个。 使用

[self presentModalViewController:modalViewController animated:YES]; 

代替。

相关问题