2

我正在尝试制作一个在地图上放置各种图钉的应用程序。

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

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

我该如何解决这个问题?

输出中的错误是:

2013-06-29 16:51:33.575 ...[2723:13d03]-[FirstViewController ...Clicked:]:无法识别的选择器发送到实例 0x867d0d0 2013-06-29 16:51:33.576 lam[2723:13d03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FirstViewController ...Clicked:]: unrecognized selector sent to instance 0x867d0d0' * First throw call stack: (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 0x1c++a 4

我的 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:@"..."]) 
4

1 回答 1

3

错误:

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

意味着它正在尝试调用该...Clicked:方法FirstViewController(因为您将advertButton' 目标设置为该方法)但没有这样的方法(您已将其注释掉)。

可以取消评论它。


但是由于您calloutAccessoryControlTapped无论如何都在使用委托方法来检测附件水龙头(比自定义方法更好的选择),您应该改为执行以下操作:

从中删除advertButton addTarget线的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应该是这样的:

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

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

    UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

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

    return MyPin;
}
于 2013-06-29T15:21:29.083 回答