Sachin Pampannavar
Sachin Pampannavar

Reputation: 13

Passing data from UITableView to the destination ViewController using Segue(Objective-C)

i'm trying to pass the data of the tableviewcell to the destination ViewController where i have a label and i want to set that data to label's text using segue when i select the particular cell.

Upvotes: 1

Views: 614

Answers (4)

Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

@property (strong, nonatomic) NSIndexPath *selectedRow; // set a default value of nil in viewDidLoad method

Now just use this logic to pass data from UITableView to any UIViewController

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    selectedRow = indexPath;
    //now simply invoke this method to perform segue.
    [self performSegueWithIdentifier:@"Your-Identifier" sender:self];
}

check your segue identifier and then pass the data.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"Your-Identifier"]) {
            UIViewController *viewController = segue.destinationViewController;
            //Your current selected cell path is stored in selectedRow if you have save value in any Array you can fetch it like this...
            viewController.firstName = [arrayOfFirstNames objectAtIndex:selectedRow.row];
            //Or You can access the tableView cell by this method.
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedRow];
        }

}

Upvotes: 0

Hardik Shah
Hardik Shah

Reputation: 179

You can use below method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SegueName"]) {
        //Create Object/Variable in destination ViewController and assign here 
    } }

You can check the Demo @ http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

Upvotes: 2

Andrea
Andrea

Reputation: 26383

It really simple:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:<#whatever#>]) {
        NSIndexPath * selectedIndexPath = [tableView indexPathForSelectedRow];
        // get from your data source the data you need at the index path
        YourVC * destVC = [segue destinationViewController];
        destVC.selectedData = data;
    } 
}

This means:

  1. Your table view has selection enable
  2. Your destination view controller has property -selectedData
  3. You have a segue that starts from the prototype table view cell to your destination view controller

Upvotes: 1

user749127
user749127

Reputation:

Simply declare a global variable in your origin view controller's .m file to store the data, and a property of the same type in your destination view controller, then store that data in the property in your -prepareForSegue method.

Your code should look like this:

@implementation OriginViewController
{
  ObjectType *object;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    object = // whatever value you want to store
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue
{
    DestinationViewController *destVC = [segue destinationViewController];
    [destVC setProperty:object];
}

Upvotes: 1

Related Questions