开发者

Set view controllers property before pushViewController

开发者 https://www.devze.com 2023-04-12 00:03 出处:网络
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. Her

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];

In the CustomViewController vi开发者_运维知识库ewDidLoad 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!

Any idea why ?

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. 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;
    
  2. 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];
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号