簡體   English   中英

我正在嘗試在地圖視圖上放置多個圖釘,但出現錯誤

[英]I am trying to drop multiple pins on a map view but getting error

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];
        }

我認為您已經通過添加觀察者來接收通知..並且當您接收到該通知並執行與該觀察者相關聯的功能時,將您的MapAnnotation取消分配,但您並未刪除該觀察者,請在dealloc中也刪除觀察者...

 [[NSNotificationCenter defaultSenter] removeObserver:self];

希望這個能對您有所幫助..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM