Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set view controllers property before 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];

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!

Any idea why ?

Stephane

like image 992
Steve Avatar asked Dec 01 '22 01:12

Steve


1 Answers

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];
    
like image 97
Vladimir Avatar answered Dec 03 '22 14:12

Vladimir