Search code examples
iosuistoryboardseguensobject

Pass data UItable to UItable Via NSObject


Can I pass the data from table to other table via NSObject ?

Example:

First table send data

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
     NSIndexPath *path = [self.tableView indexPathForSelectedRow];
     Data *g = [[Data alloc]init];
     [g setSelection1:(int)path.row];
}

NSLog test selecttion1 value = path.row;

When I use second table I import the data and alloc it but my value in selection1 is lost.

It is always 0;


Solution

  • Try this:

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    {
        if ([segue.identifier isEqualToString:@"<YOUR_SEQUE_IDENTIFIER_GOES_HERE>"]) 
        {
            <YOUR_CONTROLLER> *destController =  (<YOUR_CONTROLLER> *)  
                                                  segue.destinationViewController;
    
             NSIndexPath *path = [self.tableView indexPathForSelectedRow];
             Data *g = [[Data alloc] init]; 
             //if you would like to re-use this data source, I would suggest making this as a   
             //property within this class 
    
            [g setSelection1:(int)path.row];
            [destController setMyData:g];
        }
    }
    

    In your second table, declare a property:

    @property (strong, nonatomic) Data *myData;