2011-11-02 137 views
0

可能重複:
Why can't I push a new view controller onto the current view?
Why is my new view controller not appearing?如果我的navigationController返回「(null)」,我該怎麼辦?

我需要從我的navigationController推我的視圖控制器,但畢竟已經做了的NSLog語句來找出爲什麼什麼也沒有用出下面的代碼,我意識到它返回null:

-(IBAction)doChangePasscode{ 

NSLog(@"Change Passcode Screen Loaded!"); 

ChangePasscode *cpscreen = [[ChangePasscode alloc] initWithNibName:@"ChangePasscode" bundle:[NSBundle mainBundle]]; 
[self.navigationController pushViewController:cpscreen animated:YES]; 

NSLog(@"%@",self.navigationController); 

} 

這是怎麼發生的?我能做些什麼來返回除(null)之外的適當值?

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myRootViewController]; 

然後:

+0

什麼是你有這樣的原始控制器代碼在?它實際上是一個NavigationController嗎? – WrightsCS

+0

不知道,它只是叫做「ViewController.m」... – pixelbitlabs

+0

你可以在上面的代碼中顯示你正在初始化和顯示'self' view controller的代碼嗎? – chown

回答

1

像大家已經解釋過的和Why is my new view controller not appearing?,你需要先擁有一個導航控制器。使用該導航控制器推動您的視圖控制器,然後您的視圖控制器將不再爲navigationController屬性爲null。

在這個例子中someSecondViewController將在上述代碼中self

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 
    self.nav = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease]; 
    [rootViewController release]; 
    [self.window addSubview:nav.view]; 
    [self.window makeKeyAndVisible]; 
} 

- (void)someOtherMethod { 
    SecondViewController *someSecondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    [self.nav pushViewController:someSecondViewController animated:YES]; 
    [someSecondViewController release]; 
} 

請仔細閱讀這些:

View Controller Programming Guide for iOS
UINavigationController Docs
UIViewController Docs

0

我假設你要回到零,這意味着的viewController沒有一個導航控制器,你可以有你的viewController是一個新創建的UINavigationController像這樣從根本上解決這對於myRootViewController,self.navigationController將返回導航控制器。

我假設的viewController是在你的應用程序中的第一個,所以你想擁有這對你的appDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    MyViewController *viewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease]; 
    UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease]; 

    self.window.rootViewController = myNavController; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

然後若該IBAction爲是MyViewController self.navigationController將返回導航控制器,而不是零。

希望有所幫助。

+0

我在什麼地方說「myRootViewController」? – pixelbitlabs

+0

您想要成爲導航控制器堆棧上的第一個viewController的viewController實例。 –

+0

我把「ViewController」,但它提出了一個錯誤,並說這是一個未聲明的標識符:( – pixelbitlabs