在pushViewController之前设置视图控制器属性

在我的应用程序中,我添加了一个标签到一个视图,将它连接到一个sockets,但是当我第一次从另一个视图控制器分配这个sockets,然后调用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]; 

CustomViewController viewDidLoad方法中,我添加了这个指令来看看它是否可以工作

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

但它没有显示在标签!

任何想法为什么?

斯特凡

即使视图控制器被创build它的视图层次可能不(因此所有的子视图仍然是零),为了优化的原因,它可能不会加载,直到你试图实际访问控制器的视图。 你有两个select来解决你的问题:

  1. 将所有值存储在单独的非UIvariables中,并将其分配给UI组件,控制器将显示:

     // Before push controller vc.myPriceText = self.label_price.text; // In controller's viewWillAppear: self.lbl_price.text = self.myPriceText; 
  2. 使[视图]调用强制控制器加载其视图层次结构:

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