How to Send data between view controllers StoryBoa

2019-02-26 14:11发布

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

Xcode StoryBoard

1条回答
霸刀☆藐视天下
2楼-- · 2019-02-26 14:42

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!

查看更多
登录 后发表回答