在我的应用程序中,我向 View 添加了标签,将其连接到导出,但是当我首先从另一个 View Controller 分配该导出然后调用 pushViewController
来显示它时,没有任何显示。这是推送下一个显示标签的 View 之前的代码:
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
但它并没有显示在标签中!
知道为什么吗?
史蒂芬
最佳答案
即使创建了 View Controller ,它的 View 层次结构也可能不会(因此所有 subview 仍然为 nil),出于优化原因,它可能不会被加载,直到您尝试实际访问 Controller 的 View 。您有两种选择来解决您的问题:
将所有值存储在单独的非 UI 变量中,并将它们分配给将出现 Controller 的 UI 组件:
// Before push controller vc.myPriceText = self.label_price.text; // In controller's viewWillAppear: self.lbl_price.text = self.myPriceText;
调用 [vc view] 以强制 Controller 加载其 View 层次结构:
CustomViewController *vc = [[CustomViewController alloc] init]; [vc view]; vc.lbl_price.text = self.label_price.text; [self.navigationController pushViewController:vc];
关于iphone - 在 pushViewController 之前设置 View Controller 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7725522/