user2110495 user2110495 - 9 months ago 72
iOS Question

Pass data between view controllers WITHOUT segues

I'm using storyboard and I have a view that loads dynamic buttons. On click of this button, I need to load the second view controller and also pass data. I cannot use segue as its dynamic button.

I use following code to load the second view controller

UIStoryboard* sb1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
UIViewController* vc1 = [sb1 instantiateViewControllerWithIdentifier:@"SecondViewController"];

[self.navigationController pushViewController:vc1 animated:YES];

In the 2nd view controller, I need to access about 5 fields from the 1st view controller. How do I pass the data?

Answer Source

You can create a Segue not bounded to a button by ctrl+drag from the first controller to the second one (don't forget to give this segue and identifier).

Next In the IBAction of the button (set via Interface Builder or via addTarget:self action:forControlEvents: ) you can call the [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:button];

You can pass the data to the second controller, as usual, in - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender