2012-04-13 130 views
27

我從另一個項目複製一個工作的viewcontroller類到一個新的項目。我無法在新項目中加載視圖。在舊項目中,我使用了presentModalViewController。在新的我無法得到加載使用presentModalViewController或presentViewController的視圖無法獲得presentViewController工作

我想從我的主視圖控制器加載目前的視圖。

這裏是我的主視圖控制器界面看起來像......

// ViewController.h 
#import <UIKit/UIKit.h> 
#import "RequestDialogViewController.h" 

@interface ViewController : UIViewController <RequestDialogViewControllerDelegate> { 

} 

- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)response; 

我使用presentModalViewController這樣的...

RequestDialogViewController *requestIPViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController" bundle:nil]; 
navigationController = [[UINavigationController alloc] initWithRootViewController:requestIPViewController]; 
[self presentModalViewController:navigationController animated:YES]; 

和presentViewController這樣的...

RequestDialogViewController *requestIPViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController" bundle:nil];  
[self presentViewController:requestIPViewController animated:YES completion:nil]; 

我在新項目中缺少什麼? init方法觸發,但viewDidLoad不會顯示任何內容。

感謝

回答

73

如果ViewController是根視圖控制器,它不能在其自己的viewDidLoad中顯示模式視圖控制器,因爲此時它不具有屏幕大小等信息。

如果其他視圖控制器已經顯示,這將工作。如果根視圖控制器是UINavigationController,則當模式視圖從底部向上滑動時,您會看到一個從右側滑入的視圖。

無論如何,對於你的ViewController,最快可以呈現它是在它變得可見之後。爲此使用定時器是不可靠的;較舊和較慢的設備具有顯着較長的加載時間。

要獲得更高的可靠性,請爲ViewController實施viewDidAppear:。仍然使用你的計時器系統添加額外的延遲;幾分之一秒就足夠了。儘管在iOS 5.1模擬器中爲我提供了模式視圖控制器,但Presenting a modal view controller when loading another ViewController表示它有時不會發生。

+3

哇,這是對我不明顯。我花了一段時間試圖弄清楚這一點。謝謝。 – rjgonzo 2012-11-27 07:11:08

+0

如果在應用程序啓動時發生這種情況 - 顯示的視圖控制器顯示之前,是否仍然存在呈現視圖控制器的閃爍?任何方式在這個?有關上下文,請參閱http://stackoverflow.com/questions/14739461/how-do-you-transition-in-a-new-non-model-view-controller-without-using-uinavigat/14739540#14739540。 – Marplesoft 2013-02-07 00:17:13

+0

這仍然是iOS 10 Xcode 8 Swift 2.3的情況。謝謝! – justColbs 2017-01-09 15:40:57

0

呈現modalViewController:

對於所有的程序員開始的利益,鍵入它,而不是複製粘貼。

myVC *viewController = [[myVC alloc]initWithNibName:@"myVC" bundle:nil]; 
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
[self presentModalViewController:viewController animated:YES]; 
[viewController release]; 

看起來你試圖呈現導航控制器作爲第一樣品中的視圖控制器,則使用在所述第二個錯誤的方法。

+0

其實你是正確的presentModalViewController。我從我的工作代碼中複製了它。但是,我真的想要presentViewController工作,因爲presentModalViewController已被棄用。我確實讓它工作...看到我的答案在下面。 – user278859 2012-04-14 04:46:19

+2

爲了將來的參考,請勿複製/粘貼編程。 – 2012-11-25 02:38:35

4

我已解決。我試圖從視圖主視圖控制器的負載呈現視圖。不知道爲什麼它沒有在那裏工作,而是我現在設置它調用的方法的主視圖加載後呈現視圖控制器的計時器,它工作正常現在使用的...

[self presentViewController:requestIPViewController animated:YES completion:nil]; 

謝謝那些誰回答。

+0

此方法僅適用於iOS 6及更高版本 – 2012-11-25 02:38:05

+4

@NathanielSymer我不認爲這是正確的。文檔說presentViewController:animated:completion:是iOS5及更高版本。請參閱http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/presentViewController:animated:completion: – Marplesoft 2013-02-06 23:53:36

0

我遇到了同樣的問題。但是我的情況是presentViewController是在dismissViewControllerAnimated之後調用另一個ViewController。我的解決方案是將presentViewController移至完成塊dismissViewControllerAnimated

+0

它是有道理的。因爲你必須在提交另一個控制器之前解僱控制器。 – 2014-11-16 22:13:08

2

正如@Dondragmer所說,如果你想在根視圖的viewDidLoad中顯示你的viewController,它會失敗。一旦你的viewController準備好了,你可以展示你的新viewController。 所以,你可以做到這一點

- (void)viewDidLayoutSubviews { 
    //present here 
} 
+1

^取決於你想要展示它多少次:) Da調試器的中斷點表明這種情況會連續幾次被調用。 – 2017-10-20 21:49:34