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即使创建了视图控制器,其视图层次结构也可能不会加载(因此所有子视图仍然为零),出于优化原因,在您尝试实际访问控制器的视图之前,可能不会加载它。您有两种解决问题的方法:
// Before push controller
vc.myPriceText = self.label_price.text;
// In controller's viewWillAppear:
self.lbl_price.text = self.myPriceText;
CustomViewController *vc = [[CustomViewController alloc] init];
[vc view];
vc.lbl_price.text = self.label_price.text;
[self.navigationController pushViewController:vc];