2013-03-02 77 views
0

我知道這個問題被問了100次,但我無法弄清楚我做錯了什麼。在視圖之間傳遞數據不起作用

這是我的第二個iOS應用程序。我想通過在我的ViewControllerA中按下按鈕來修改ViewControllerB中的標籤。

  1. 我取得了一個故事板從Segue前我的按鈕(ViewControllerA)我ViewControllerB,並把它稱爲「TestSegue」。
  2. ViewControllerA.h
  3. #import "ViewControllerB.h我在ViewControllerA.m寫了這個代碼:

- (空)prepareForSegue:(UIStoryboardSegue *)賽格瑞發件人:(ID)發送{

if([segue.identifier isEqualToString:@"TestSegue"]){ 

ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController; 
controller.TestLabel.text = @"TEST!";}} 

在我ViewControllerB沒有任何反應與TestLabel ...

任何人可以看到世界衛生大會我做錯了嗎?

在此先感謝

編輯: 廠現在由於rpledge。

新代碼:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

if([segue.identifier isEqualToString:@"TestSegue"]){ 

ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController; 
controller.TestString = @"TEST!";}} 

回答

1

It'l可能是因爲沒有到viewDidLoad中創建爲textLabel UI元素運行時,這將prepareForSegue後發生:

我不就像公開無論如何公開UI視圖元素,我建議你爲你的NSString *添加@property到目標視圖控制器,然後在viewDidLoad中設置UILabel.text:

+0

唉......你說得對。那很愚蠢。非常感謝! – Michael 2013-03-02 17:49:30

0

不要將它分配給控件,而是分配給一個實例變量。

@property (nonatomic) NSString *var; 

然後在viewDidLoad中將此分配給控件。

相關問題