gpt4 book ai didi

iphone - MKUserLocation 蓝色 userLocation MKAnnotation 如果不经意触摸会导致应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 18:48:40 27 4
gpt4 key购买 nike

我有一个 MKMap 和一系列 MKAnnotations,所有这些都是红色的,这很好。我在 IB 中选择了“显示用户位置”并将 MKAnnotation 从红色更改为蓝色,我的 viewForAnnotation 方法中有代码:

if (annotation == theMap.userLocation)
return nil;

一切都很好,应用程序工作正常,但如果用户无意中点击了蓝色的用户位置点,我会遇到以下崩溃:

2012-02-01 20:43:47.527 AusReefNSW[27178:11603] -[MKUserLocationView setPinColor:]: unrecognized selector sent to instance 0x79b0720
2012-02-01 20:43:47.528 AusReefNSW[27178:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKUserLocationView setPinColor:]: unrecognized selector sent to instance 0x79b0720'
*** First throw call stack:

如果我删除上面的代码,一切正常,但引脚是红色的。我更喜欢蓝色图标,但尚未发现崩溃的原因。任何想法,将不胜感激。谢谢。

解决了!谢谢马文,这是代码,以防有人发现它有用。简而言之,我必须首先检查 MKAnnotation 是 MyAnnotation 类还是 MKUserLocation 类。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view
{
theAnnotationSelected = [[mapView selectedAnnotations] objectAtIndex:0];
if ([theAnnotationSelected isKindOfClass:[MyAnnotation class]] )
{
view.pinColor = MKPinAnnotationColorGreen;

}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKPinAnnotationView *)view
{
if ([theAnnotationSelected isKindOfClass:[MyAnnotation class]] )
{
view.pinColor = MKPinAnnotationColorRed;
}

最佳答案

对于当前用户位置,请转到 MKMapView 属性并选择“在 XIB 中显示用户位置”。

然后在 Controller 中实现 MKMapViewDelegate.. 并在 Controller 中编写此方法

-(MKAnnotationView *)mapView:(MKMapView *)pmapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil; //return nil to use default blue dot view

if([annotation isKindOfClass:[MKAnnotationClass class]])
{
//Your code
}
}

还有这个

- (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)customAnnotationView
{

if(![annotation isKindOfClass:[MKUserLocation class]])
{
//Your code
}

}

使用此功能,您可以在用户位置上看到蓝点..

关于iphone - MKUserLocation 蓝色 userLocation MKAnnotation 如果不经意触摸会导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9093690/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com