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

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

知道为什么吗?

Stephane

推荐答案

即使创建了视图控制器,它的视图层次结构也可能不会(因此所有子视图仍然是零),出于优化原因,在您尝试实际访问控制器视图之前可能无法加载它。您有两种方法可以解决您的问题:

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天全站免登陆