Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

performSegueWithIdentifier and sharedData

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

like image 403
Ondrej Avatar asked Dec 10 '11 17:12

Ondrej


3 Answers

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.

like image 173
rob mayoff Avatar answered Oct 26 '22 12:10

rob mayoff


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
}
like image 28
Johnnywho Avatar answered Oct 26 '22 12:10

Johnnywho


When you want data back from second to first, better way is to use Unwind Segues.

like image 36
Amit Vyas Avatar answered Oct 26 '22 13:10

Amit Vyas