I have a tableview called “FirstViewController”, and when I select either cell in the table, it will push to “SecondViewController”. SecondViewController has a UILabel named “randomTitle”, that I need to change based on the selected cell on FirstViewController.
So if the selected cell is named “a random row”, I need to change the UILabel to that.
I tried this so far without much luck. This goes in the FirstViewController:
SecondViewController *secondView = [[SecondViewController alloc] init];
secondView.randomTitle.text = @"Test"; // hardcoding it for now
[self.navigationController pushViewController:secondView animated:YES];
But the label does not change. What am I missing? The nib is properly connected. This is what the SecondViewController’s .h looks like:
@interface SecondViewController : UIViewController
{
UILabel *randomTitle;
}
@property (nonatomic, retain) IBOutlet UILabel *randomTitle;
Congrats, you have just run into something which leads to such freaking bugs, and this something is called “you can call any methods on nil with no problem”.
When you set the text to your label, it is not set into outlet yet (as controller didn’t start to load its view; it would do right after you push it).
To keep your code clean and maintenable I would suggest creating a property to hold the text (or, better, the model associated with the selected row) and use it in viewWillAppear of the second controller to configure its presentation.
P.S. To prove me right just put text assigning after the push 🙂