Frage

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;

War es hilfreich?

Lösung

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;

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top