Search code examples
iosuibuttonmkmapviewmapkitsender

send object in click button in mkMapView ios


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 ?


Solution

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