在我的应用程序中,我向视图添加了标签,将其连接到插座,但当我第一次从另一个视图控制器分配此插座然后调用时,什么也没有显示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];
In the CustomViewController
viewDidLoad
方法我添加了这条指令来看看它是否应该工作
NSLog(@"Price=%@",lbl_price); // it actually prints out what was previously assigned
但它没有显示在标签中!
知道为什么吗?
Stephane
即使创建了视图控制器,它的视图层次结构也可能不会(因此所有子视图仍然为零),出于优化原因,在您尝试实际访问控制器的视图之前,它可能不会被加载。您有两种选择来解决您的问题:
-
将所有值存储在单独的非 UI 变量中,并将它们分配给带有控制器的 UI 组件,即将出现:
// Before push controller
vc.myPriceText = self.label_price.text;
// In controller's viewWillAppear:
self.lbl_price.text = self.myPriceText;
-
调用 [vc view] 来强制控制器加载其视图层次结构:
CustomViewController *vc = [[CustomViewController alloc] init];
[vc view];
vc.lbl_price.text = self.label_price.text;
[self.navigationController pushViewController:vc];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)