设置视图控制器属性pushViewController
问题描述:
在我的应用程序之前,我已经添加了一个标签视图,它连接到一个插座,但没有显示出来,当我第一次从另一个视图控制器分配此插座,然后调用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];
在CustomViewController
viewDidLoad
方法添加此指令,看看它是否应该工作
NSLog(@"Price=%@",lbl_price); // it actually prints out what was previously assigned
但它并不显示在标签!
任何想法,为什么?
斯特凡
答
即使视图控制器创建的视图层次结构可能不会(和因此所有子视图仍然是零),优化的原因,可能无法加载,直到您尝试实际访问控制器的视图。您有两个选项来解决问题:
-
商店都在单独的非UI变量的值,并将它们分配与控制器的UI组件将会出现:
// Before push controller vc.myPriceText = self.label_price.text; // In controller's viewWillAppear: self.lbl_price.text = self.myPriceText;
-
让[VC视图]强制控制器加载其视图层次结构:
CustomViewController *vc = [[CustomViewController alloc] init]; [vc view]; vc.lbl_price.text = self.label_price.text; [self.navigationController pushViewController:vc];