Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在pushViewController之前设置视图控制器属性_Iphone_Ios_Ios4_Uiviewcontroller_Uinavigationcontroller - Fatal编程技术网

Iphone 在pushViewController之前设置视图控制器属性

Iphone 在pushViewController之前设置视图控制器属性,iphone,ios,ios4,uiviewcontroller,uinavigationcontroller,Iphone,Ios,Ios4,Uiviewcontroller,Uinavigationcontroller,在我的应用程序中,我向视图添加了一个标签,将其连接到插座,但当我首先从另一个视图控制器分配此插座,然后调用pushViewController显示它时,什么也没有显示。以下是按下显示标签的下一个视图之前的代码: CustomViewController *vc = [[CustomViewController alloc] init]; vc.lbl_price.text = self.label_price.text; // lbl_price is defined as a property

在我的应用程序中,我向视图添加了一个标签,将其连接到插座,但当我首先从另一个视图控制器分配此插座,然后调用
pushViewController
显示它时,什么也没有显示。以下是按下显示标签的下一个视图之前的代码:

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
但它没有显示在标签上

知道为什么吗


Stephane

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

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

    // Before push controller
    vc.myPriceText = self.label_price.text;
    
    // In controller's viewWillAppear:
    self.lbl_price.text = self.myPriceText;
    
  • 调用[vc view]以强制控制器加载其视图层次结构:

     CustomViewController *vc = [[CustomViewController alloc] init];
     [vc view];
     vc.lbl_price.text = self.label_price.text; 
     [self.navigationController pushViewController:vc];