How to Send data between view controllers StoryBoard Xcode

那年仲夏 提交于 2019-12-20 03:34:09

问题


Please help me..

I want to pass data between different controllers in storyboard. For Example.

First View Controller

NSString *firstValue;

firstValue = @"First Number";

Now I want to send this to the result view controller and store in

NSString *resultFromFirstVC;

and show in label.

[label setText: resultFromFirstVC];

so the label show:

First Number


回答1:


Use this method to pass the property in all of your view controllers

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"ShowNewVC"]) {
        NextViewController *nextVC = (NextViewController *)[segue destinationViewController];
        nextVC.someProperty = self.myProperty;
    }
}

Change the name of "NextViewController" to match yours. Be sure to add the "Segue Identifiers" in your Storyboard file.

This should work. However, when you have to pass this property trough so many view controllers, you can consider creating a Singleton "Data Manager" object to store the data. The View Controllers would have access to the data through the singleton.

Good luck!



来源:https://stackoverflow.com/questions/14140679/how-to-send-data-between-view-controllers-storyboard-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!