2012-04-25 57 views
0

我有一個導航控制器和2個視圖控制器。第一個View Controller與一個名爲ViewController的UIViewController相關聯。第二個連接到名爲BookVC的UIViewController。 BookVC具有的UITextField並經由出口連接:在MVC中連接數據

@property (strong, nonatomic) IBOutlet UITextField *textFieldContent; 

的連接是與使用賽格瑞按鈕製成。我想在二者之間傳遞一些數據,現在用的是下面的代碼失敗:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
BookVC* nextPage = [[BookVC alloc] init]; 
nextPage = [segue destinationViewController]; 
[email protected]"Some content"; 
} 

我應該如何通過視圖控制器之間的數據?

回答

0

我認爲問題在於textFieldContent在那時不存在。您需要將一個屬性添加到BookVC,以便將要放入的文本放入textFieldContent ...我們將其稱爲@property NSString *textFieldContentText。那麼,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    BookVC *nextPage = [segue destinationViewController]; 
    [nextPage setTextFieldContentText:@"Some content"]; 
    ... 
} 

,然後在-viewDidLoad方法BookVC

- (void)viewDidLoad 
{ 
    [textFieldContent setText:[self textFieldContentText]]; 
} 
+0

我覺得我幾乎沒有。我認爲發送文件應該有: BookVC * nextPage = [segue destinationViewController]; UITextField * tf; tf.text = @「一些內容」; [nextPage setTextFieldContent:tf]; 但是,我不明白viewDidLoad語句。 \t [nextPage setTextFieldContent:tf]; – SimonRH 2012-04-26 02:48:39

+0

第一個控制器不知道第二個控制器的文本字段。它只是設置將最終放置到'BookVC'的'textFieldContent'中的字符串。 'BookVC'的'-viewDidLoad'方法在加載時會自動調用。此時,您知道textFieldContent標籤存在,並且其文本可以設置,因此您可以在此處執行此操作。查看'UIViewController'和'-viewDidLoad'方法的文檔。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html – macserv 2012-04-26 03:05:56

+0

忽略最後的評論。你的解釋是完美的。謝謝! – SimonRH 2012-04-26 03:16:22

0

你不需要這行:

BookVC* nextPage = [[BookVC alloc] init]; 

,創建一個全新的BookVC你然後去和下一行覆蓋。

由於您正在使用[segue destinationViewController],您應該很好。當你延續到下一頁時會發生什麼?