我正在尝试制作一个应用程序,在 map 上放置各种图钉。
当我触摸图钉时,标准气泡会弹出,并带有一个披露按钮。当我单击披露按钮时,我希望它打开一个新的 ViewController,以便我可以显示更多信息。
问题是,每次我运行该应用程序时,它都会崩溃,并在输出中给出错误。
我该如何解决这个问题?
输出的错误是:
2013-06-29 16:51:33.575 ...[2723:13d03] -[FirstViewController ...Clicked:]: unrecognized selector sent to instance 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 0x1d45) libc++abi.dylib: terminate called throwing an exception
我的 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:@"..."])
最佳答案
错误:
[FirstViewController ...Clicked:]: unrecognized selector sent to instance
意味着它正在尝试调用 ...Clicked:
方法FirstViewController
(因为您将 advertButton
的目标设置为该方法)但没有这样的方法(您已将其注释掉)。
您可以取消评论。
但由于您使用的是 calloutAccessoryControlTapped
无论如何,委托(delegate)方法来检测附件点击(比自定义方法更好的选择),您应该执行以下操作:
删除 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;
}
关于ios - 在 map View 中向图钉添加操作时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17381017/