I have a UITableView that pushes, via a storyboard segue, a view, which displays a UILabel that I wish to change the text on relative to the indexPath.row of the selected accessory on the UITableView.
I know it’s probably wildly wrong, but this was my attempt. I feel like I’m going about it very wrong:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"ArticlePreviewSegue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [sender indexPathForSelectedRow];
ArticlePreviewViewController *apvc = [segue destinationViewController];
NSDictionary *article = [_newsFetcher.articles objectAtIndex:indexPath.row];
apvc.titleLabel.text = [article objectForKey:@"title"];
apvc.bodyLabel.text = [article objectForKey:@"body"];
}
Thanks you!
One problem may be that tapping the accessory doesn’t select the row. You can handle that by passing the index path as the sender of the segue:
Now you can access the index path in
prepareForSegue:sender:without relying on the row being selected.Another problem is that in
prepareForSegue:sender:,apvchasn’t loaded its view yet. Soapvc.titleLabelandapvc.bodyLabelare both nil.The proper way to handle this is to give
ArticlePreviewViewControlleranarticleproperty and set that property inprepareForSegue:sender:, like this:Then, in
-[ArticlePreviewViewController viewDidLoad], you can set the labels based on the article: