在 pushViewController 之前设置视图控制器属性 [英] Set view controllers property before pushViewController

查看:30
本文介绍了在 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:

  1. 将所有值存储在单独的非 UI 变量中,并将它们分配给 UI 组件,其中将出现控制器:

  1. 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 之前设置视图控制器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆