在mkMapView ios中单击按钮发送对象

时间:2014-06-02 17:41:30

标签: ios uibutton mkmapview mapkit sender

在我的Mapview中我有一个注释,annontion有一个按钮,他将我发送到另一个视图以获取有关此注释的更多详细信息

我的代码是

myAnnotation *myAnn = (myAnnotation *)annotation;
    UIButton *discloseButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
    [discloseButton addTarget: self action: @selector(showInfo:) forControlEvents: UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = discloseButton;
    UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:myAnn.image]];
    UIImage *pinImage = [UIImage imageNamed:@"pointer.png"];
    [annotationView setImage:pinImage];
    annotationView.leftCalloutAccessoryView = leftIconView;
    return annotationView;

-(IBAction)showInfo:(id)sender
{
    [self performSegueWithIdentifier:@"aa" sender:sender];
}

// envoie des données vers la detail d'une mission
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"aa"])
    {
        DetailViewController *detailviewcontroller = [segue destinationViewController];
        detailviewcontroller.DetailModal = @[@"aa",@"aaa",@"aaa"];
    }
}

如何通过点击按钮填充我的表detailviewcontroller.DetailModal来发送对象注释?

2 个答案:

答案 0 :(得分:1)

您可以将所有数据发送到格式字符串,并在prepareForSegue

中拆分字符串

像这样的代码

myAnnotation *myAnn = (myAnnotation *)annotation;
UIButton *discloseButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
[discloseButton addTarget: self action: @selector(showInfo:) forControlEvents: UIControlEventTouchUpInside];

NSString *detail = [NSString stringWithFormat:@"%@::%@::%@::%@::%@::%@::%@::%@::%@::%@::%@"
        ,myAnn.title,myAnn.description,myAnn.Latitude,myAnn.longitude,myAnn.image,myAnn.type,myAnn.ville,myAnn.point,myAnn.nombreDeVisite,myAnn.nbVTotal,myAnn.idMission];


discloseButton.accessibilityHint = detail;
annotationView.rightCalloutAccessoryView = discloseButton;

在您的函数prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"aa"])
    {
        UIButton *button = (UIButton *)sender;
        NSArray* infos = [button.accessibilityHint componentsSeparatedByString: @"::"];
        if ([infos count] > 1)
        {
            tit = [infos objectAtIndex:0];
            description = [infos objectAtIndex:1];
            Latitude = [infos objectAtIndex:2];
            longitude = [infos objectAtIndex:3];
            image = [infos objectAtIndex:4];
            type = [infos objectAtIndex:5];
            ville = [infos objectAtIndex:6];
            point = [infos objectAtIndex:7];
            nombreDeVisite = [infos objectAtIndex:8];
            nbVTotal = [infos objectAtIndex:9];
            idMission = [infos objectAtIndex:10];
        }
        DetailViewController *detailviewcontroller = [segue destinationViewController];
        detailviewcontroller.DetailModal = @[tit,description,Latitude,longitude,image,type,ville,point,nombreDeVisite,nbVTotal,idMission];
    }
}

答案 1 :(得分:0)

使用mapview的委托方法:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
而不是分配给按钮的自己的动作

功能示例代码:

#import "ViewController.h"
#import <MapKit/MapKit.h>

@interface ViewController () <MKMapViewDelegate>
@end

@interface Test : NSObject<MKAnnotation>
@property MKMapView *mapView;
@end

@implementation Test

-(NSString *)title {
    return @"test";
}

- (CLLocationCoordinate2D)coordinate {
    return self.mapView.centerCoordinate;
}

@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.delegate = self;
    [self.view addSubview:mapView];

    Test *test = [[Test alloc] init];
    test.mapView = mapView;
    [mapView addAnnotation:test]; //just as a test
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"test"];
    UIButton *discloseButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
    discloseButton.tag = 1;
    annotationView.canShowCallout = YES;
    annotationView.rightCalloutAccessoryView = discloseButton;
    return annotationView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    NSLog(@"annotationView %@", view);
    NSLog(@"tag  of click %d", control.tag);
    NSLog(@"annotation %@", view.annotation);
}
@end