2

In My Mapview i have sum annotation the annontion have a button who sent me to another view for more detail on this annotation

My code is

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;

and

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

How can I send the object annotation via click the botton for filling my table detailviewcontroller.DetailModal ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ti Amo Laky
  • 755
  • 2
  • 8
  • 21

2 Answers2

1

you can send all your data to format string and split your string in your prepareForSegue

the code like this

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;

in your function 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];
    }
}
Ryodo
  • 445
  • 5
  • 17
  • This is an extremely crude and fragile solution. The delimited detail string is completely unnecessary and very non-object-oriented. It completely defeats the benefit of the custom annotation class object `myAnnotation` that the OP has defined. See http://stackoverflow.com/questions/14805954/mkannotationview-push-to-view-controller-when-detaildesclosure-button-is-clicked and http://stackoverflow.com/questions/20986571/passing-data-from-annotations-to-detail-view-ios-using-storyboard for a better approach. –  Jun 05 '14 at 12:56
0

use mapview's the delegate method:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
instead of your own action assigned to the button

functional example code:

#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
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135