Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...