Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the value of a UILabel following a segue?

Tags:

ios

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??

like image 611
Tony Stark Avatar asked Mar 06 '12 00:03

Tony Stark


1 Answers

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.

like image 69
jonkroll Avatar answered Oct 14 '22 15:10

jonkroll