-1

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;

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kana
  • 5
  • 2
  • A common method for passing data from one `UIViewController` to another is by adding a property to the destination view controller, and during `prepareForSegue` set the value of the property on the destination view controller. In the code example provided, I don't see the setting of a property value on the destination view controller. – bobnoble Oct 06 '13 at 10:25
  • So your data did get passed to the second table, but when switching back to first table, the data is lost? Can you clarify? – Unheilig Oct 06 '13 at 10:42

2 Answers2

0

Your Data object will not serve you because you haven't passed it to the destination view controller.

You can do it like this

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

     MyTargetViewController *targetVC = segue.destinationViewController;
     [targetVC setData:g];
}

Here replace "MyTargetViewController" with your actual target view controller.

Gaurav Singh
  • 1,897
  • 14
  • 22
0

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;
Unheilig
  • 16,196
  • 193
  • 68
  • 98