I’ve got a ViewController, that has a UIButton which performs the following:
- (IBAction)buttonClicked:(id)sender
{
NSLog(@"Button clicked, lets move to next controller to do stuff");
[self performSegueWithIdentifier:@"toNextController" sender:nil];
}
This just moves onto my next ViewController, nothing amazing so far.
In the second ViewController, I will do some of my application logic, then return.
- (IBAction)backButtonPressed:(id)sender
{
NSLog(@"Back button clicked, lets just drop out of here...");
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)saveButtonPressed:(id)sender
{
NSLog(@"save button clicked, lets send some data back");
[self performSegueWithIdentifier:@"backToMain" sender:nil];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"backToMain"])
{
NSLog(@"Preparing segue for backToMain");
// Obtain handles on the current and destination controllers
MainController * startingViewController;
SecondController * destinationController;
startingViewController = (MainController * ) segue.sourceViewController;
destinationController = (SecondController * ) segue.destinationViewController;
// set data on the main controller
startingViewController.myString = @"SomeDummyString";
}
}
What I have tried to do so far, is to create a second segue that links back to the main controller, and before performing the segue, grab a handle on it and set data. I’m not sure if that is the best way of navigating back or not.
Question:
Is it possible, to return data when doing a [self dismissModalViewControllerAnimated:YES];, or do you need to implement a segue for the return journey?
Check this out: 10841897
It describes using a delegate and protocol to send the information back and forth. It can be slightly changed to fit your needs, just create a
savedWithData:method that sends a dictionary or whatever data you want back to the first view controller rather than just the genericdonewhich is described in the link.