2011-10-11 123 views
3

在我的應用程序之前,我已經添加了一個標籤視圖,它連接到一個插座,但沒有顯示出來,當我第一次從另一個視圖控制器分配此插座,然後調用pushViewController來顯示它。下面是顯示標籤推動下一個視圖之前代碼:設置視圖控制器屬性pushViewController

CustomViewController *vc = [[CustomViewController alloc] init]; 
vc.lbl_price.text = self.label_price.text; // lbl_price is defined as a property in CustomViewController and label_price is defined in current view controller 
[self.navigationController pushViewController:vc]; 

CustomViewControllerviewDidLoad方法添加此指令,看看它是否應該工作

NSLog(@"Price=%@",lbl_price); // it actually prints out what was previously assigned 

但它並不顯示在標籤!

任何想法,爲什麼?

斯特凡

回答

18

即使視圖控制器創建的視圖層次結構可能不會(和因此所有子視圖仍然是零),優化的原因,可能無法加載,直到您嘗試實際訪問控制器的視圖。您有兩個選項來解決問題:

  1. 商店都在單獨的非UI變量的值,並將它們分配與控制器的UI組件將會出現:

    // Before push controller 
    vc.myPriceText = self.label_price.text; 
    
    // In controller's viewWillAppear: 
    self.lbl_price.text = self.myPriceText; 
    
  2. 讓[VC視圖]強制控制器加載其視圖層次結構:

    CustomViewController *vc = [[CustomViewController alloc] init]; 
    [vc view]; 
    vc.lbl_price.text = self.label_price.text; 
    [self.navigationController pushViewController:vc];