奇怪的pushViewController行为

时间:2013-02-08 23:47:32

标签: objective-c xcode

我的应用程序从LoginViewController开始。 “登录”视图有一个“注册”按钮,代码如下所示。 当我点击注册时应用程序启动,没有任何反应。但是如果我登录和注销,那么注册按钮就能正常工作。

这会发生什么问题?

的AppDelegate:

...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    //Load View Controllers;

    self.vcClubs = [[[ClubsViewController alloc] init] autorelease];
    self.vcParties = [[[PartiesViewController alloc] init] autorelease];
    self.vcEditProfile = [[[EditProfileViewController alloc] init] autorelease];

    self.navClubs = [[[UINavigationController alloc] initWithRootViewController:self.vcClubs] autorelease];
    self.navClubs.navigationBarHidden = YES;

    self.navParties = [[[UINavigationController alloc] initWithRootViewController:self.vcParties] autorelease];
    self.navParties.navigationBarHidden = YES;

    // Load Login View
    UIViewController *loginView = [[[LoginViewController alloc] init] autorelease];
    self.window.rootViewController = loginView;
    [self.window makeKeyAndVisible];

    return YES;
}
...
- (void)login
{
self.vcMenu = [[[MenuViewController alloc] init] autorelease];
self.vcLocationMenu = [[[LocationMenuViewController alloc] init] autorelease];
self.mainController = [[[MainViewController alloc] init] autorelease];
self.mainController.leftViewController = self.vcMenu;
self.mainController.rightViewController = self.vcLocationMenu;

[self.mainController setMainViewController:self.navParties];
[self.mainController showMainViewControllerAnimated:YES];

self.window.rootViewController = self.mainController;

CATransition *transition = [[[CATransition alloc] init] autorelease];
transition.duration = 1;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromBottom;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.window.layer addAnimation:transition forKey:@"login"];
}
...

LoginController.php:

...
- (IBAction)onTapSignupButton:(id)sender
{
RegisterViewController *controller = [[[RegisterViewController alloc] init] autorelease];
[self.navigationController pushViewController:controller animated:YES];
NSLog(@"%@", self.navigationController);
}
...

在应用开始时返回NSLOG:

2013-02-18 02:18:29.986 App[32652:c07] (null)

4 个答案:

答案 0 :(得分:2)

看起来可能很奇怪。但我做了同样的错误。希望它可以帮助你..

检查您是否在AppDelegate中初始化了UInavigationController。如果是的话我不知道你为什么会这样...如果没有尝试下面的代码

RegisterViewController *controller = [[[RegisterViewController alloc] init] autorelease];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:controller]
[self.navigationController pushViewController:controller animated:YES];    

根据您编辑的代码

 // Load Login View
UIViewController *loginView = [[[LoginViewController alloc] init] autorelease];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:loginView]
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];

快乐编码!!!

答案 1 :(得分:1)

似乎第一次尝试时,LoginViewController没有被包裹在导航控制器中。我会检查登录/注销周期是否将登录控制器放在导航堆栈中,并查看为什么在第一次登录时没有发生这种情况

答案 2 :(得分:0)

试试这个,

RegisterViewController *controller = [[[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];

[self.navigationController pushViewController:controller animated:YES];

答案 3 :(得分:0)

RegisterViewController *controller = [[[RegisterViewController alloc] init] autorelease];
[self.navigationController pushViewController:controller animated:YES];

由于Objective C内存管理,您的代码不正确 首先,您应该检查导航控制器是否为零。 然后^正确的代码看起来像

RegisterViewController *controller = [[RegisterViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
[controller release];