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 之前设置视图控制器属性
基础教程推荐
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
