I hope I'm not asking something that's been already answered (but I found no answer to this, so hopefully I'm not).
I have an app in the current xcode version, using segues and navigationController. I need to pass data from one view to the other - what's the easiest way to do this? I ran onto some sharedData thing that could be possibly hooked onto the performSegueWithIdentifier method but don't know how to use it (or whether it is the right choice to do it like this).
Thanks
A segue has two view controllers: sourceViewController
and destinationViewController
. When UIKit executes a segue, it sends a prepareForSegue:sender:
message to the source VC. You can override that method in your view controller subclass to pass data to the destination VC.
For example, suppose you have a master view controller with a table view of movies, and when the user clicks a row in the table view, you want to segue to a detail view controller for the movie.
@implementation MasterViewController
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *detailVC = segue.destinationViewController;
NSIndexPath *selectedPath = [self.tableView indexPathForSelectedRow];
detailVC.movie = [self movieForIndexPath:selectedPath];
}
This is explained in the Introducing Interface Builder Storyboarding video from WWDC 2011.
It's also worth noting that when the segue's origin is a table view cell, or the accessory button of a table view cell, the sender
argument of prepareForSegue:sender:
is the table view cell.
I think the best way is to import header for the view controller that will be shown in controller that is performing segue. And then use it's accessors or other methods to pass needed data inside prepareForSegue:
// In FirstViewController.h
#import "SecondViewController.h"
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"SegueToSecondViewController"]) {
// Get destination view controller and don't forget
// to cast it to the right class
SecondViewController *secondController = [segue destinationViewController];
// Pass data
secondController.dataArray = self.someDataArray;
secondController.name = @"Fancy name";
}
}
When you want data back from second to first, I suggest to use delegate:
// In FirstViewController.h
#import "SecondViewController.h"
#import "SecondViewControllerDelegate.h"
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"SegueToSecondViewController"]) {
SecondViewController *secondController = [segue destinationViewController];
// Declare first view controller as a delegate
secondController.delegate = self;
// Pass data
secondController.dataArray = self.someDataArray;
secondController.name = @"Fancy name";
}
}
// Second controller's delegate method,controller
// ie. used to return data after second view is dismissed
- (void)secondControllerFinishedSomeTask:(NSArray *)someReturnedData {
// Do something with returned data
}
When you want data back from second to first, better way is to use Unwind Segues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With