Set view controllers property before pushViewController(在 pushViewController 之前设置视图控制器属性)
问题描述
在我的应用程序中,我为视图添加了标签,将其连接到插座,但当我第一次从另一个视图控制器分配此插座然后调用 pushViewController 以显示它时,什么都没有显示.这是推送下一个显示标签的视图之前的代码:
In my app I've added a label to a view, connected it to an outlet but nothing shows up when I first assign this outlet from another view controller and then call pushViewController to display it. Here's the code before pushing next view that display the label:
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 方法中我添加了这条指令,看看它是否应该工作
In the CustomViewController viewDidLoad method I added this instruction to see if it should work
NSLog(@"Price=%@",lbl_price); // it actually prints out what was previously assigned
但它没有显示在标签中!
But it doesn't show into the label!
知道为什么吗?
斯蒂芬
推荐答案
即使创建了视图控制器,它的视图层次也可能不会(因此所有子视图仍然为零),出于优化原因,它可能在您尝试之前不会加载实际访问控制器的视图.您有两种选择来解决您的问题:
Even if view controller is created its view hierarchy may not (and so all subviews will still be nil), for optimization reasons it may not be loaded until you try to actually access controller's view. You have two options to solve your problem:
将所有值存储在单独的非 UI 变量中,并将它们分配给 UI 组件,其中将出现控制器:
Store all values in separate non-UI variables and assign them to UI components with controller is going to appear:
// Before push controller
vc.myPriceText = self.label_price.text;
// In controller's viewWillAppear:
self.lbl_price.text = self.myPriceText;
调用 [vc view] 以强制控制器加载其视图层次结构:
Make [vc view] call to force controller to load its view hierarchy:
CustomViewController *vc = [[CustomViewController alloc] init];
[vc view];
vc.lbl_price.text = self.label_price.text;
[self.navigationController pushViewController:vc];
这篇关于在 pushViewController 之前设置视图控制器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 pushViewController 之前设置视图控制器属性
基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
