iphone - 我正在尝试在 map View 上放置多个图钉,但出现错误

标签 iphone objective-c ios

for (int i = 0; i < self.businessArray.count; i++) {
        Business *business = [self.businessArray objectAtIndex:i];
        MapAnnotation *mapAnnotation = [[MapAnnotation alloc] init]; 
        NSLog(@"%f %f", business.coordinate.latitude, business.coordinate.longitude);
        mapAnnotation.title = business.name;
        mapAnnotation.subtitle = business.address1;
        mapAnnotation.coordinate = business.coordinate;
        [bMapView addAnnotation:mapAnnotation];
        NSLog(@"ti %@", mapAnnotation.title);
        NSLog(@"sub %@", mapAnnotation.subtitle);
        NSLog(@"coo %f %f", mapAnnotation.coordinate.latitude, mapAnnotation.coordinate.longitude);
    }

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

    if (annotation != mapView.userLocation) {
        static NSString *defauleID = @"myLocation";
        //        pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:defauleID];
        if (pinView == nil) {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier:defauleID];
        }
        pinView.pinColor = MKPinAnnotationColorGreen;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        [rightButton addTarget:self 
                        action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = rightButton;
    }

    return pinView;
}

错误:
取消分配了类MapAnnotation的实例0x1cdd97b0,同时仍向其注册了键值观察器。观察信息已泄漏,甚至可能错误地附加到其他对象上。在NSKVODeallocateBreak上设置一个断点以在调试器中停止。这是当前的观察信息:
(
上下文:0x0,属性:0x1cd97a30>

最佳答案

每次在循环中释放注释

    for (int i = 0; i < self.businessArray.count; i++) {
            Business *business = [self.businessArray objectAtIndex:i];
            MapAnnotation *mapAnnotation = [[MapAnnotation alloc] init]; 
            NSLog(@"%f %f", business.coordinate.latitude, business.coordinate.longitude);
            mapAnnotation.title = business.name;
            mapAnnotation.subtitle = business.address1;
            mapAnnotation.coordinate = business.coordinate;
            NSLog(@"ti %@", mapAnnotation.title);
            NSLog(@"sub %@", mapAnnotation.subtitle);
            NSLog(@"coo %f %f", mapAnnotation.coordinate.latitude, mapAnnotation.coordinate.longitude);
            [bMapView addAnnotation:mapAnnotation];
            [mapAnnotation release];
        }

关于iphone - 我正在尝试在 map View 上放置多个图钉,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11221291/

相关文章:

iphone - 如何检测 iPhone 上的环境声级?

ios - 如何在 Swift 上使用 RESTKIT 处理 JSON 的 Null 映射

ios - NavigationBar 在 Show Detail segue 后不会消失

ios - 在 iOS 中通知 A 类关于 B 类的最佳方式

iphone - iOS 上的 WiFi 可能性

ios - 我可以提交不支持 4s 的 iOS 应用程序吗?

ios - 核心数据 : Timeout adding persistent store on application launch

iphone - View Controller 的全局实例是否应该由应用程序委托(delegate)拥有

java - 在 Eclipse 中使用代号一

ios - 如何在 UISegmentedControl 值更改时仅更改文本颜色?