Call another view when user taps on pin in mapview

£可爱£侵袭症+ 提交于 2019-12-12 05:48:30

问题


I have Map with multiply annotations and I want hen user taps on pin to call another view.

I'm using storyboard.

With .xib this would be like this.

...
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
...
[rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
}

-(void)showDetails:(UIButton*)sender {

     OfferDetailViewController *detail = [[OfferDetailViewController alloc] init];

     detail.context = context;

     Offers *offer = [arr objectAtIndex:indexPath.row];

     //detail.offer = offer;

    [self.navigationController pushViewController:detail animated:YES];
}

...

回答1:


You can create a Segue between the views and give it an identifier in StoryBoard. Then execute "performSegueWithIdentifier" to trigger it. So in your method which is called by the user tapping you call the "performSegueWithIdentifier" something like this if your Segue has an identifier of "FrameSegue"

[self performSegueWithIdentifier:@"FrameSegue" sender:sender];

You can setup the transition in "prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender" if you need do any setup first to pass data to the other view.

I hope that helps.

Answer to your comment below :-

If you have a Segue Identified as "FrameSegue" and you want to pass a string from the view "MyFirstView" to the second view called "MySecondView" and store it in myData in the second view.

Try this:

In MyFirstView.m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"FrameSegue"]) {

        MySecondView *secondView = [segue destinationViewController];
        [secondView setMyData:myString];
    }
}

- (IBAction)tappedThing:(id)sender {

    NSString *myString = @"A String";

    [self performSegueWithIdentifier:@"FrameSegue" sender:sender];

}



回答2:


Implement the mapView:annotationView:calloutAccessoryControlTapped: method and put your showDetail: implementation in there instead, possibly doing some conditional tests for the annotation view in question.



来源:https://stackoverflow.com/questions/10834047/call-another-view-when-user-taps-on-pin-in-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!