2013-02-19 83 views
-1

當試圖執行這條線,位於一個單獨的函數:錯誤呈現的ViewController

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

我收到此錯誤:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

我的聲明和實例都位於同一個文件

@class typesel_vc; 
@interface 
@property(nonatomic,strong)typesel_vc *selectVC; 

@implementation 
@synthesize selectVC=selectVC_; 

-(void)viewDidAppear:(BOOL)animated{ 
    selectVC_=[[typesel_vc alloc]init]; 
} 

有關如何處理此錯誤的任何想法?

編輯:

實際行,我叫presentViewController

selectVC_=[[typesel_vc alloc]init]; 
[self presentViewController:selectVC_ animated:YES completion:nil]; 
+0

能否請您提供調用堆棧? – dlackty 2013-02-19 18:38:47

+1

問題很簡單 - 在您調用presentViewController時,您的'selectVC_' ivar是'nil'。你需要確定原因。 – rmaddy 2013-02-19 18:41:54

+0

你的代碼清單中的@interface之後似乎缺少一個類名 – 2013-02-19 18:47:40

回答

3

首先,類名按照慣例應該以大寫字母開頭之前放置在分配權。其次,在加載控制器的視圖時,初始化將來呈現的模式視圖控制器是沒有意義的。你應該在出示之前這樣做。

三,從您發佈的代碼中無法確定錯誤。使用日誌語句和斷點來遍歷代碼,以查看nil對象的出現位置。

+0

你在這裏陳述的一切都是很好的信息,但這真的不是問題的答案。這並不能解釋爲什麼會發生這樣的事故。 – rmaddy 2013-02-19 18:44:46

+0

您沒有包含足夠的信息。我的答案提供瞭解決這個常見問題的適當方法。 – Mundi 2013-02-19 18:46:55

+0

@rmaddy查看最後一段 - 「從您發佈的錯誤碼無法確定」... – 2013-02-19 18:47:03

0

我能夠通過修改這些代碼來解決此問題:

selectVC_=[[typesel_vc alloc]init]; 
[self presentViewController:selectVC_ animated:YES completion:nil]; 

要:

selectVC_=[self.storyboard instantiateViewControllerWithIdentifier:@"typesel_vc"]; 
[self presentViewController:selectVC_ animated:YES completion:nil]; 
+0

有趣。通過將nil傳遞給'presentViewController:animated:completion:'你應該得到一個'NSInvalidArgumentException - 應用程序試圖在目標上呈現一個零模式視圖控制器..'。每天學習一些東西。 – Jeremy 2013-02-19 19:56:27