Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables between view controllers

I am using this function to switch between views in Xcode 4.3.

[self performSegueWithIdentifier:@"NextView" sender:self];

I would like to pass some parameters between pages. How can I do that?

like image 457
Bob Avatar asked Jul 20 '12 10:07

Bob


People also ask

How do you pass a string value from one viewController to another view controller?

To pass data back, the most common approach is to create a delegate property in your detail view controller, like this: class ViewControllerB: UIViewController { var selectedName: String = "Anonymous" weak var delegate: ViewControllerA! }

How do I pass value from one viewController to another in swift?

Control + click the UI element you are going to use to make the bridge and drag to the second View Controller. Select the “Show” option from the “Action Segue” menu. Control + click the button and drag to the second ViewController, then select “Show.”


2 Answers

This question has a very nice and correct explanation of passing data between view controllers:

Passing Data between View Controllers

like image 85
Kaan Dedeoglu Avatar answered Oct 27 '22 00:10

Kaan Dedeoglu


After performSegueWithIdentifier:sender: your view controler will call

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

assuming your new view controller has some propertys to set:

if ([[segue identifier] isEqualToString:@"NextView"]) {
    MyViewController *myVC = [segue destinationViewController];
    myVC.propertyToSet = // set your properties here
}
like image 38
Pfitz Avatar answered Oct 26 '22 23:10

Pfitz