prepareForSegue passes value for Index 0…ALWAYS

拟墨画扇 提交于 2019-12-12 01:54:48

问题


I have an array of items displayed in a TableViewController. They display properly in the TVC. The code below segues, but it only segues to indexPath 0 of my array of MKMapItems, not the item from the cell that was clicked.

Any thoughts on where my mistake is?

@property (strong, nonatomic) NSArray *mapItems;

Each cell has a "Add POI" UIButton which that triggers a segue created with Interface Builder called "addPOISegue".

Here's the IBAction for the "Add POI" button:

- (IBAction)addPOIButtonClicked:(UIButton *)sender {
    NSLog(@"Add POI button clicked");
    [self performSegueWithIdentifier:@"addPOISegue" sender:sender];
}

Here's the `prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton*)sender {

//    NSLog(@"The sender is %@", sender);
    if ([[segue identifier] isEqualToString:@"addPOISegue"]) {
        AddPOIViewController *destinationVC = segue.destinationViewController;

        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:sender.center];
        NSLog(@"NSIndexPath *indexPath's index is %@", indexPath);
        MKMapItem *item = _mapItems[indexPath.row];
//        NSLog(@"ResultsTVC item is %@", item);
        destinationVC.item = item;
    }

}

The indexPath keeps getting set to 0. I suspect this is because I've got a button triggering the segue within the cell, but I'm stumped as to how to resolve this.


回答1:


You should delete the button's action method, and connect the segue directly from the button to the next controller. In prepareForSegue, you can pass the button's origin, after converting it to the table view's coordinate system to the indexPathForRowAtPoint: method to get the indexPath of the cell that button is contained in,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton*)sender {

    if ([[segue identifier] isEqualToString:@"addPOISegue"]) {
        AddPOIViewController *destinationVC = segue.destinationViewController;
        CGPoint p = [sender convertPoint:CGPointZero toView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
        NSLog(@"NSIndexPath *indexPath's index is %@", indexPath);
        MKMapItem *item = _mapItems[indexPath.row];
//        NSLog(@"ResultsTVC item is %@", item);
        destinationVC.item = item;
    }

}



回答2:


@rdelmar figured it out. I needed a CGPoint due to the UIButton contained in my cell and referenced within my prepareForSegue method.

This code got it working:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton*)sender {

//    NSLog(@"The sender is %@", sender);
    if ([[segue identifier] isEqualToString:@"addPOISegue"]) {
        AddPOIViewController *destinationVC = segue.destinationViewController;

        CGPoint point = [sender convertPoint:CGPointZero toView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
        NSLog(@"NSIndexPath *indexPath's index is %@", indexPath);
        MKMapItem *item = _mapItems[indexPath.row];
        NSLog(@"ResultsTVC item is %@", item);
        destinationVC.item = item;
    }

}


来源:https://stackoverflow.com/questions/28968942/prepareforsegue-passes-value-for-index-0-always

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