I have a simple storyboard consisting of two UIViewControllers, with a segue connecting them.
UIVC1 --> UIVC2
I'm trying to set a UILabel on UIVC2 to be equal to a string stored in UIVC1. I'm trying to pass the string in the prepareForSegue
method, and thus far I've set it to a property
in UIVC2.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"mySegue"]) {
[segue.destinationViewController setDesc:[Brain description]];
}
}
The property in UIVC2 is desc.
Then, in my setDesc
method, which I've implemented, I run self.display.text = self.desc
where display
is my property for the UILabel.
However, this is not working, and even when I just NSLog the value of the UILabel, it doesn't print anything, which makes me wonder if the controller is even communicating with the UILabel... (I did do the ctr+click and drag thing in the storyboard to hook them up.)
Is there a better way to do this??
Your UILabel text is not being set because prepareForSegue
is being called before the IBOutlet for your label on the destination view controller is connected. So at that point the label is still nil
(and sending a message to nil
has no effect.)
What you should do instead is to create a new property on your destination view controller to store the string, and set this property in prepareForSegue
. Then in your destination view controller's viewDidLoad
method, use the value of that property to set the text
property of your UILabel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With