如何从地图引脚上的披露按钮调用segue? [英] How to call a segue from a disclosure button on a map pin?
问题描述
我有一个应用程序在mapview上绘制引脚。
I have an app that plots pins on a mapview.
每个引脚都使用此代码显示详细信息披露按钮,当点击时调用showDetail方法然后调用prepareForSegue方法。我觉得这里有很多额外的工作。
Each pin uses this code to display a detail disclosure button, which when tapped calls a showDetail method which then calls the prepareForSegue method. Im thinking there is a lot of extra work here.
我应该消除showDetail并调用prepareForSegue方法吗?但是我如何传递MyLocation对象?
Should I eliminate the showDetail and just call the prepareForSegue method? but how would I pass in the MyLocation object?
以下是代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
//test if mapviewnil
if (_mapView == nil) {
NSLog(@"NIL");
}
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image=[UIImage imageNamed:@"locale.png"];
//instatiate a detail-disclosure button and set it to appear on right side of annotation
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:@selector(showDetailView:annotation:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = infoButton;
return annotationView;
}
return nil;
}
-(void)showDetailView:(id <MKAnnotation>)annotation{
//Set the data
MyLocation *sendingLocation = annotation;
DetailViewController *destinationViewController = segue.destinationViewController;
destinationViewController.receivedLocation = sendingLocation;
[self performSegueWithIdentifier: @"DetailVC" sender: self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"DetailVC"]) {
NSLog(@"DetailVC called");
} else {
NSLog(@"PFS:something else");
}
}
提前Thx!
推荐答案
通常你不会为按钮添加目标,而只需设置 rightCalloutAccessoryView
(像你一样)然后写一个 calloutAccessoryControlTapped
方法,例如:
Usually you'd not add a target for the button, but rather just set the rightCalloutAccessoryView
(like you have) and then write a calloutAccessoryControlTapped
method, e.g.:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"locale.png"]; // since you're setting the image, you could use MKAnnotationView instead of MKPinAnnotationView
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
} else {
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
if (![view.annotation isKindOfClass:[MyLocation class]])
return;
// use the annotation view as the sender
[self performSegueWithIdentifier:@"DetailVC" sender:view];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(MKAnnotationView *)sender
{
if ([segue.identifier isEqualToString:@"DetailVC"])
{
DetailViewController *destinationViewController = segue.destinationViewController;
// grab the annotation from the sender
destinationViewController.receivedLocation = sender.annotation;
} else {
NSLog(@"PFS:something else");
}
}
这篇关于如何从地图引脚上的披露按钮调用segue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!