Problems passing data to a DetailViewController from TableView in iOS5

最后都变了- 提交于 2019-12-04 11:33:42

In your destination view controller set up a property (public) so you can set the value you wish to pass in. Implement prepareForSegue in your origin view controller and import the header of your destination view controller.

To get the indexPath of a table view cell you should have an IBOutlet set to your table view, this way you can get the indexPath using the indexPathForCell method in your prepareForSegue method.

In your prepareForSegue you should have something like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  if ([segue.identifier isEqualToString:@"[identifier for your segue]"]) {
     [segue.destinationViewController setValueIWantToSet:value];
  }
}

You could also set the value of the label text here, if you wish.

The reason your viewDidLoad finishes before is because iOS sets up the destination view controller before transitioning to it, using the prepareForSegue method guarantees that your destination view controller is already instantiated and set up, so you can set the values you wish.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!