Gangani Roshan Gangani Roshan - 3 years ago 162
Objective-C Question

used of undeclared identifier "indexPath"

I will try to pass data between two controller using segue method but it's give me error like


used of undeclared identifier "indexPath"


Check My Code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *index = (NSIndexPath *)sender;
NameViewController *destViewController = [segue destinationViewController];
destViewController.receiptName = [self.nameListArray objectAtIndex:indexPath.row]; // here display the error.
}
}

Answer Source

According to the thrown exception the sender is obviously the table view cell rather than the index path.

Then you have to write

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell: (UITableViewCell*)sender];
        NameViewController *destViewController = [segue destinationViewController];
        destViewController.receiptName = self.nameListArray[indexPath.row];
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download