在 Core Data 应用程序中进行 segueing 时 prepareForSegue 崩溃

prepareForSegue crashes when segueing in Core Data app

我有一个 tableViewController 设置来显示有关 managedObjectContext 中对象的数据。我没有从编译器中得到任何错误,程序运行正常,直到我单击单元格中的 UIButton 以继续。

我正在尝试挑选出与 managedObjectContext 中的对象关联的纬度和经度值,并将这些值传递给另一个 VC。

这是我的 prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"directionsSegue"]) {
        // Set destination view controller
        DirectionsViewController *destinationVC = segue.destinationViewController;

        // Pick out the "thing" you want to send to destinationVC
        CGPoint point = [sender convertPoint:CGPointZero toView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
        NSLog(@"NSIndexPath *indexPath's index is %@", indexPath);

        // Set the "thing" on the destinationVC
        PointOfInterest *poi = [self.fetchedResultsController objectAtIndexPath:indexPath];
        destinationVC.destinationLatitude = poi.latitude;
        destinationVC.destinationLongitude = poi.longitude;

    }
}

这是我的控制台日志:

2015-03-15 12:01:18.449 BlocSpot[11680:832245] savedPOI managedObjectContext is <NSManagedObjectContext: 0x7fad650b1e60>
2015-03-15 12:20:12.719 BlocSpot[11680:832245] -[SavedPOITableViewController convertPoint:toView:]: unrecognized selector sent to instance 0x7fad62c6f640
2015-03-15 12:20:12.728 BlocSpot[11680:832245] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SavedPOITableViewController convertPoint:toView:]: unrecognized selector sent to instance 0x7fad62c6f640'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010955eb95 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000108e3dbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010956606d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001094bc1bc ___forwarding___ + 988
    4   CoreFoundation                      0x00000001094bbd58 _CF_forwarding_prep_0 + 120
    5   BlocSpot                            0x000000010836ae54 -[SavedPOITableViewController prepareForSegue:sender:] + 276
    6   UIKit                               0x000000010a0d471c -[UIStoryboardSegueTemplate _perform:] + 151
    7   BlocSpot                            0x000000010836ad1b -[SavedPOITableViewController directionButton:] + 91

更新

添加以下代码设置我的CGPoint后:

    UIView *view = [(SavedPOITableViewController *)sender view];

它走得更远了一点,但它仍然崩溃。这是日志:

2015-03-15 12:39:50.073 BlocSpot[11915:852339] savedPOI managedObjectContext is <NSManagedObjectContext: 0x7ffaac51d850>
2015-03-15 12:39:51.661 BlocSpot[11915:852339] NSIndexPath *indexPath's index is <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
2015-03-15 12:39:51.662 BlocSpot[11915:852339] -[UIButton view]: unrecognized selector sent to instance 0x7ffaac5eb330
2015-03-15 12:39:51.665 BlocSpot[11915:852339] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton view]: unrecognized selector sent to instance 0x7ffaac5eb330'

*** First throw call stack:
(
    0   CoreFoundation                      0x000000010d2dbb95 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010cbbabb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010d2e306d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010d2391bc ___forwarding___ + 988
    4   CoreFoundation                      0x000000010d238d58 _CF_forwarding_prep_0 + 120
    5   BlocSpot                            0x000000010c0e7dcf -[SavedPOITableViewController prepareForSegue:sender:] + 207
    6   UIKit                               0x000000010de5171c -[UIStoryboardSegueTemplate _perform:] + 151

在这一行

CGPoint point = [sender convertPoint:CGPointZero toView:self.tableView];

您正在 SavedPOITableViewController 上调用 convertPoint:toView:,但这不是您的视图 控制器 的方法,而是视图的方法.试试这个:

CGPoint point = [self.view convertPoint:CGPointZero toView:self.tableView];