Home > DeveloperSection > Forums > Getting indexpath in prepareForSegue
Mark Devid
Mark Devid

Total Post:101

Points:707
Posted on    October-18-2014 12:16 AM

 iPhone iPhone  Ios 
Ratings:


 1 Reply(s)
 1216  View(s)
Rate this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    {
    if ([segue.identifier isEqualToString:@"Action"])
    {
        NSIndexPath *indexPath = [self.tbl indexPathForSelectedRow];
        SecondViewController *destViewController = segue.destinationViewController;
        destViewController.getString = [getArray objectAtIndex:indexPath.row];
    }
}
i wanna to access the selected row index,but show null for every selected row. please help me?


Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    October-18-2014 12:17 AM

Two cases:

1)Segue connected from the viewController

Call segue from your didSelectRowAtIndexPath method, pass indexPath as sender

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"Action" sender:indexPath];
}
Then you can get indexPath as sender in prepareForSegue:sender: method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
 {
     if ([segue.identifier isEqualToString:@"Action"])
      {
           NSIndexPath *indexPath = (NSIndexPath *)sender;
           SecondViewController *destViewController = segue.destinationViewController;
           destViewController.getString = [getArray objectAtIndex:indexPath.row];
      }
  }
2)segue connected from the cell

No need to implement didSelectRowAtIndexPath method and performSegueWithIdentifier:.You can directly get sender as UITableviewCell in prepareForSegue:sender: method.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"Action"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        SecondViewController *destViewController = segue.destinationViewController;
        destViewController.getString = [getArray objectAtIndex:indexPath.row];
    }
}

Don't want to miss updates? Please click the below button!

Follow MindStick