2014-02-26 34 views
2

我有兩個賽段離開了我的初始視圖控制器。 push segue連接到單元格,模式連接到+ UIBarButtonItem。prepareForSegue在調用時不發送參數,導致崩潰

enter image description here

當這是我prepareForSegue的代碼是什麼樣子,一切都很好,filepath是通過推SEGUE發送,但文件路徑不通過模式SEGUE通過。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showTimes"]) { 
     [[segue destinationViewController] setFilePath:filePath]; 
    } 

// if ([[segue identifier] isEqualToString:@"addEvent"]) { 
//  [[segue destinationViewController] setFilePath:filePath]; 
// } 
} 

如果我取消註釋第二個if語句和segue標識符,則應用程序崩潰。 filePath無處發送。我已經嘗試將[[segue destinationViewController] setFilePath:filePath];移出兩個if語句,以確保該行被執行,但這也會使我的應用程序崩潰。

這兩個seg都在屬性中設置了正確的標識符。但是,我不確定Xcode實際上是否意識到設置了正確的標識符,因爲當我更改segue類型時,下次運行應用程序時不會反映這些更改。

我不知道該怎麼做。 Objective-C相對而言是新手。

+1

更換第二,如果有別的,如果 – zaheer

+0

我試過'別人if'太多,但它給出了相同的結果時,它的註釋掉的。我試過把它改成'else',但是這似乎也不起作用。 – waterproofpiano

+0

查看下面的Jason Coco的答案,如果這不起作用,請分享日誌中顯示的崩潰原因。 – zaheer

回答

4

0123uesegue指向導航控制器,而不是您的視圖控制器。相反,讓您的視圖控制器,並設置文件路徑:

[[[segue destinationViewController] topViewController] setFilePath:filePath]; 
+0

現在編譯器有一個setFilePath的問題:並說「沒有可見的@interface for'UiViewController'聲明選擇器'setFilePath:'我有目標視圖控制器導入和filePath在每個.h文件中聲明。 ? – waterproofpiano

+1

您必須將UIViewController強制轉換爲特定的子類,供編譯器瞭解-setFilePath方法,即'[(SubclassName *)[[segue destinationViewController] topViewController] setFilePath:filePath];' –

+1

當然,一個類假定知道destinationViewController的類型並不是完全鬆散耦合的......使用它們之間的委託協議會更加標準,您可以將目標設置爲源的委託,然後調用委託方法將filePath作爲參數傳遞參數,然後執行segue。 –