user777304
user777304

Reputation: 455

How to Send data between view controllers StoryBoard Xcode

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

Upvotes: 2

Views: 10315

Answers (1)

Nikola Kirev
Nikola Kirev

Reputation: 3152

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!

Upvotes: 5

Related Questions