2

我想知道presentModalViewController和navigationController

[self presentModalViewController:controller animated:YES];

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

我都用了,但仍然不知道或注意到的差異之間的差異。何時應該使用其中之一?

謝謝..

+1

Apple開發者文檔摘要:通常,應用程序使用模態視圖控制器作爲臨時中斷,以便從用戶獲取關鍵信息。導航控制器用於管理分層數據的表示。或者是你的問題「幕後發生了什麼?」 – wegginho 2011-04-20 09:52:15

+0

所以我可以說,如果我想顯示當前的不同視圖,並返回到當前視圖,那麼我應該使用presentModalViewController? – Maulik 2011-04-20 10:17:40

+0

如果不同意味着「你想用這張圖片做什麼? - >上傳 - >刪除 - >取消」這個問題,那麼在你回到你的照片後,是的。 – wegginho 2011-04-20 10:21:12

回答

2

呈現模態的視圖被呈現於另一視圖的頂部的視圖。您通常執行那些需要以獨立方式啓動和完成的「任務」。閱讀蘋果開發者指南上的模態視圖。

在應用程序中存在導航的邏輯需求時,將導航視圖添加到導航控制器是不同的。請在iDevices的設置應用程序中向下鑽取表格,其中有主要設置,然後深入到子設置等。

無論您的問題是什麼,如果它們是概念性和泛型的,我強烈建議你到google了「X編程指南」將帶你到正確的蘋果的編程指南:) X =視圖控制器,你的情況

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

3

基本區別:

pushViewController只能在個導航控制器

presentModalViewController作品所有視圖控制器

navigationController是你UINavigationController,這是在你的導航堆棧(UIViewController)用於所有的控制器的實例。

2

如果基類有它自己的NavigationController那麼你可以這樣寫:

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

如果你的基類只有UIViewController的再使用:

MyViewController * objMyViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil]; 
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:objMyViewController]; 
navController.navigationItem.leftBarButtonItem = nil; // make nil if you want 
                 // to use it in next View 
[self presentModalViewController:navController animated:YES]; 

現在,MyViewController有導航,所以你可以 - 通過在MyViewController中編寫函數,可以推送另一個viewController。

-(IBAction)btnNext_click { 
    SecondViewController * objSecondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 
    [self.navigationController pushViewController:objSecondViewController animated:YES]; 
} 
+0

但第二個視圖將顯示在模態視圖控制器後面 – 2cupsOfTech 2013-07-11 07:06:47