0

I am attempting to use a UIViewController (View Controller) with a table view to send data to another UIViewController (Detail View Controller). My code looks like this:

//ViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"grapedetail"];
    detail.grape = [self.grapes objectAtIndex:indexPath.row];
    currentGrape = [self.grapes objectAtIndex:indexPath.row];
    NSLog(@"%@",currentGrape.searchName);
    [self.navigationController pushViewController:detail animated:YES];
    [self performSegueWithIdentifier:@"toGrapeDetail" sender:self];
}

//DetailViewController.m
- (void)viewDidLoad
{
    //obtain grape from table view
    //grape = ((ViewController *)self.presentingViewController).currentGrape; //I have also tried this method for obtaining the variable with the same result
    NSLog(@"Detail view says: %@",grape.searchName);
    //etc...
}

After I build the app I go to the View Controller and select a table cell to go to the Detail View Controller. Here is my output log:

2012-07-23 08:13:27.430 GrapeKeeper[593:f803] Detail view says: (null)
2012-07-23 08:13:27.432 GrapeKeeper[593:f803] Alarije

From this it seems that the detail view viewDidLoad is being called before the original VC's didSelectRowAtIndexPath, even though I clearly want the latter to happen first so that the variable passes correctly. What can I do to fix this?

4

1 回答 1

1

是的,当我第一次遇到它时,这似乎非常奇怪,但它就是这样。

诀窍是用 inprepareForSegue:sender:而不是didSelectRowAtIndexPath:. 这样,您将目标控制器作为 segue 的目的地。

(不过,您的代码看起来确实有点困惑,您是在推动控制器还是让 segue 来做。)

于 2012-07-23T12:29:37.207 回答