在注释数组上设置公开按钮

时间:2014-07-22 13:10:05

标签: ios nsarray mkmapview uisegmentedcontrol mkannotation

我无法弄清楚如何在一组注释上设置公开按钮。

注释分为3种阵列。

NSMutableArray *category1 = [[NSMutableArray alloc]init];
NSMutableArray *category2 = [[NSMutableArray alloc]init];
NSMutableArray *category3 = [[NSMutableArray alloc]init];

我在这些数组中添加注释对象,如下所示:

myAnn = [[Annotations alloc]init];
location.latitude = 52.381285;
location.longitude = 4.888740;
myAnn.coordinate = location;
myAnn.title = @"";
myAnn.subtitle = @"";
[category1 addObject:myAnn];

将它们放在另一个阵列中:

[self.locationArrays addObject:category1];

我使用分段控件在地图上显示3组 currentAnnotation是一个使用objectAtIndex的整数。

-(IBAction)setMap:(id)sender
{
    int newAnnotations = ((UISegmentedControl *) sender).selectedSegmentIndex;

    if (newAnnotations != self.currentAnnotation)
    {
        [self.myMapView removeAnnotations:[self.locationArrays objectAtIndex:self.currentAnnotation]];
        [self.myMapView addAnnotations:[self.locationArrays objectAtIndex:newAnnotations]];
        self.currentAnnotation = newAnnotations;

        [self.myMapView showAnnotations:[self.locationArrays objectAtIndex:newAnnotations] animated:YES];
    }
}

我的问题是: 例如,catergory1中的对象如何获得披露按钮? 我应该如何将它与viewForAnnotationcalloutAccessoryControlTapped方法一起使用,因为我已经将所有注释都包含在左侧蓝色汽车配件中。


更新

ViewForAnnotation方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{   
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];
    if (!annotationView)
    {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapVC"];
        annotationView.canShowCallout = YES;

        if ([annotation isKindOfClass:[MKUserLocation class]] || annotation == myMapView.userLocation || annotation == locationManager)
        {
            return nil;
        }
        else
        {
            //Blauw Navigatie Auto...
            UIImageView *carView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Driving"]];
            UIButton *blueView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44+30)];
            blueView.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
            carView.frame = CGRectMake(11, 14, carView.image.size.width, carView.image.size.height);
            [blueView addSubview:carView];
            annotationView.leftCalloutAccessoryView = blueView;
        }

        if ([annotation isKindOfClass:[Annotations class]])
        {
            Annotations *myRightAnn = (Annotations *)annotation;
            if (myRightAnn.category == 1)
            {
                annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            }
            else
            {
                annotationView.rightCalloutAccessoryView = nil;
            }
        }
    }
    return annotationView;
}

1 个答案:

答案 0 :(得分:1)

在您的Annotations课程(似乎是MKAnnotation实施的课程)中,添加名为&#34; category&#34; (该名称​​没有与&#34; Objective-C类别&#34; - 它仅仅是指你所谓的三个数组:category1。 .3,如果您愿意,请使用其他名称:

@interface Annotations : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, assign) int category;     //<-- add this property

@end


创建注释时,将注释的category属性设置为1,2,3或其他:

myAnn = [[Annotations alloc]init];
location.latitude = 52.381285;
location.longitude = 4.888740;
myAnn.coordinate = location;
myAnn.title = @"";
myAnn.subtitle = @"";
myAnn.category = 1;     //<-- set to 1 because adding to category1 array
[category1 addObject:myAnn];


然后在viewForAnnotation中,检查注释的category属性并根据需要设置rightCalloutAccessoryView(下面的只是对代码的粗略概念):

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return;
    }

    static NSString *reuseId = @"ann";
    MKAnnotationView *av = [mapView dequeueReusable...
    if (av == nil) {
        av = [[MKAnnotationView alloc] initWithAnnotation...
        av.image = ...
        av.leftCalloutAccessoryView = ...
        av.canShowCallout = YES;
        //Set other properties here that will be
        //the same for ALL annotations...
    }
    else {
        av.annotation = annotation;
    }


    //Set rightCalloutAccessoryView based on annotation **AFTER**
    //the dequeue/alloc+init...

    if ([annotation isKindOfClass:[Annotations class]]) {
        Annotations *myAnn = (Annotations *)annotation;
        if (myAnn.category == 1) {
            //set right view to button only for category 1...
            av.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
        else {
            //no right view for any other categories...
            av.rightCalloutAccessoryView = nil;
        }
    }

    return av;
}


calloutAccessoryControlTapped中,检查注释的类,将其强制转换为Annotations并在必要时检查category并根据需要进行处理:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    if ([view.annotation isKindOfClass:[Annotations class]]) {
        Annotations *myAnn = (Annotations *)view.annotation;

        if (control == view.leftCalloutAccessoryView) {
            //handle left control tap...
        }
        else
            if (control == view.rightCalloutAccessoryView) {
                //handle right control tap...
            }
    }
}