iphone - 在 pushViewController 之前设置 View Controller 属性

标签 iphone ios ios4 uiviewcontroller uinavigationcontroller

在我的应用程序中,我向 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 。您有两种选择来解决您的问题:

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

    // Before push controller
    vc.myPriceText = self.label_price.text;
    
    // In controller's viewWillAppear:
    self.lbl_price.text = self.myPriceText;
    
  2. 调用 [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/

相关文章:

objective-c - iBooks 中打开关闭(展开)书籍动画

iOS:重新加载 TableView 不起作用

iphone - 创建自己的键盘的代码示例

iphone - 在应用程序钥匙串(keychain)中存储 In App Purchase 收据

iphone - 如何调整 rightBarButtonItems 中两个 UIBarButtonItem 之间的间距

iphone - 混合模式,可自由绘制重叠的UIColor

iphone - 使用页面控制?

objective-c - 如何将 JSON 解析为 Objective C - SBJSON

ios - 为什么设备越狱后 NSFileProtectionKey 不具有属性?

iphone - 在 iOS 5 上获取区域设置