在地图视图中向引脚添加操作时出错

时间:2013-06-29 14:05:04

标签: ios mapkit

我正在尝试创建一个应用程序,其中各种引脚放在地图上。

当我触摸一个引脚时,标准气泡会弹出一个公开按钮。当我单击我的公开按钮时,我希望它打开一个新的ViewController,以便我可以显示更多信息。

问题是,每次运行应用程序时,它都会崩溃并且它会在输出中出现错误。

我该如何解决这个问题?

输出中的错误是:

  

2013-06-29 16:51:33.575 ... [2723:13d03] - [FirstViewController   ...单击:]:无法识别的选择器发送到实例0x867d0d0   2013-06-29 16:51:33.576 lam [2723:13d03] *由于终止应用程序   未捕获的异常'NSInvalidArgumentException',原因:   ' - [FirstViewController ... Clicked:]:发送无法识别的选择器   例子0x867d0d0'   * 第一次抛出调用堆栈:(0x1d8d012 0x11cae7e 0x1e184bd 0x1d7cbbc 0x1d7c94e 0x11de705 0x182c0 0x18258 0xd9021 0xd957f 0xd86e8 0x47cef   0x47f02 0x25d4a 0x17698 0x1ce8df9 0x1ce8ad0 0x1d02bf5 0x1d02962   0x1d33bb6 0x1d32f44 0x1d32e1b 0x1ce77e3 0x1ce7668 0x14ffc 0x1e12   0x1d45)libc ++ abi.dylib:terminate调用抛出异常

我的FirstViewController.m中的代码是:

[...]

[...]

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
    MyPin.pinColor = MKPinAnnotationColorPurple;

    UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    if ([[annotation title] isEqualToString:@"..."]) {
        [advertButton addTarget:self action:@selector(...Clicked:) forControlEvents:UIControlEventTouchUpInside];
    }

    if ([[annotation title] isEqualToString:@"..."]) {
        [advertButton addTarget:self action:@selector(...Clicked:) forControlEvents:UIControlEventTouchUpInside];
    }

    MyPin.rightCalloutAccessoryView = advertButton;
    MyPin.draggable = NO;
    MyPin.highlighted = YES;
    MyPin.animatesDrop=TRUE;
    MyPin.canShowCallout = YES;

    return MyPin;
}

- (void)mapView:(MKMapView *)mapView MyPin:(MKPinAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    // Go to edit view
    UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"...Controller" bundle:nil];
    [self.navigationController pushViewController:detailViewController animated:YES];
}


//-(void) ...Clicked:(id)sender {

  //  NSLog(@"... Clicked");
//}


-(void) ...:(id)sender {

    NSLog(@"...");
}


-(IBAction)findmylocation:(id)sender {

    mapView.showsUserLocation = YES;
    mapView.delegate = self;
    [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

}

导致错误的原因是:

- (void)mapView:(MKMapView *)mapView MyPin:(MKPinAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    // Go to edit view
    UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"..." bundle:nil];
    [self.navigationController pushViewController:detailViewController animated:YES];
}

编辑2: 我已经删除了advertButton addTarget行,但是现在我运行模拟器时没有透露按钮...是我删除了正确的吗?

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
    MyPin.pinColor = MKPinAnnotationColorPurple;

    UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    if ([[annotation title] isEqualToString:@"..."])

    if ([[annotation title] isEqualToString:@"..."]) 

1 个答案:

答案 0 :(得分:3)

错误:

  

[FirstViewController ... Clicked:]:无法识别的选择器发送到实例

表示它试图在...Clicked:上调用FirstViewController方法(因为您将advertButton的目标设置为该方法)但是没有这样的方法(您已评论过)它出来了。

可以取消评论。


但是,由于您使用calloutAccessoryControlTapped委托方法来检测附件点击(比自定义方法更好的选择),您应该执行以下操作:

移除 来自advertButton addTarget的{​​{1}}行,只需处理viewForAnnotation中的点击。

同时删除calloutAccessoryControlTapped方法。


另一个问题是方法...Clicked:被错误地命名为calloutAccessoryControlTapped

方法 必须 这样命名为mapView:MyPin:calloutAccessoryControlTapped:

mapView:annotationView:calloutAccessoryControlTapped:


顺便说一句,在- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { // Go to edit view UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"...Controller" bundle:nil]; [self.navigationController pushViewController:detailViewController animated:YES]; } 中,您将能够访问通过calloutAccessoryControlTapped点击的注释。


更新的view.annotation应该是这样的:

viewForAnnotation