user2110495 user2110495 - 1 month ago 18
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"
bundle:nil];
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

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