1

我試圖從UINavigationController中得到一種「從左到右」的分層導航。我使用此代碼:如何讓UIBarButtonItem調用「pushViewControllerAnimated:(BOOL)」到UINavigationController?

-(IBAction)showSettings:(id)sender { 
UINavigationController *aNavController = [[UINavigationController alloc] initWithNibName:nil bundle:nil]; 
[self presentModalViewController:aNavController animated:YES]; 

SecondView *secondView = [[SecondView alloc] initWithNibName:nil bundle:nil]; 
[aNavController pushViewController:secondView animated:NO]; 

UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithTitle:@"Knapp" style:UIBarButtonItemStylePlain target:self action:@selector(nextView:)]; 
aNavController.topViewController.navigationItem.rightBarButtonItem = aButton; 

[aButton release]; 
[secondView release]; 
[aNavController release]; } 

-(IBAction)nextView:(id)sender { 
ThirdView *thirdView = [[ThirdView alloc] initWithNibName:nil bundle:nil]; 
[self.navigationController pushViewController:thirdView animated:YES]; 
NSLog(@"Push it like it's hot"); } 

「新思維」被正確地叫,「推它,喜歡它的熱」,印在「輸出」,但沒有被按下時,沒有新的視圖顯示出來。我也嘗試改變: [self.navigationController pushViewController:thirdView animated:YES];[self.navigationController popViewControllerAnimated:YES];但結果保持不變,沒有任何反應。

我的結論是這樣的:我在'nextView'的self.navigationController部分做了錯誤的事,即程序不知道從/到什麼推/彈出。我已經盡力去查看某種有關這方面的文檔,但是我無法解決,所以我就是這樣。

是self.navigationController做到這一點的正確方法,或者我錯過了該行的內容?

預先感謝您, 託拜厄斯Tovedal

回答

1

當使用self.navigationController,您沒有訪問在showSettings定義爲aNavController導航控制器。 self.navigationController是指可以推送自我視圖控制器的導航。我假定這個代碼被定義的視圖沒有NavigationController。這意味着self.navigationController指向空。 您可以通過執行NSLog(@"%@", self.navigationController);

你需要做的是讓aNavController實例變量在你的頭文件,然後你可以在你的新思維做[aNavController pushViewController:thirdView animated:YES];在新思維的方法進行驗證:方法。

+0

蘇海爾!你絕對正確,'NSLog(@「%@」,self.navigationController);'輸出'(null)'。在頭文件中添加'aNavController'作爲一個變量解決它,謝謝你的幫助! /托比亞斯 –

0

什麼似乎是你的問題是

UINavigationController *aNavController = [[UINavigationController alloc] initWithNibName:nil bundle:nil]; 
[self presentModalViewController:aNavController animated:YES]; 

推一個新的模型視圖控制器堆棧,並且不會使這個導航控制器當前視圖控制器的導航控制器;

因此您需要將當前視圖控制器導航控制器設置爲aNavController或將aNavController設置爲類變量並使用它來推送第三個視圖。

+0

是的,這是正確的,謝謝你的回答邁克爾! –

相關問題