您可以从提供的MKAnnotationView 访问数据
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
view 对象有一个annotation 属性,它将为您提供一个采用MKAnnotation 协议的对象。这可能是您已经拥有的MKPointAnnotation,如果只有title 和subtitle 就可以。但是你也可以定义一个自定义注解类来保存status 和company:
MyAnnotation *annotation = view.annotation;
// annotation.status
// annotation.company
您必须创建一个MyAnnotation 实例并将数据插入到您当前正在创建newAnnotation 的位置。
一旦你有了你需要的数据并且你想将它传递给 DetailViewController,我建议查看this SO answer 或Ole Begemann's tips here。简而言之,您可以创建详细视图控制器的公共属性,然后执行以下操作:
DetailViewController *destinationController = [[DestinationViewController alloc] init];
destinationController.name = annotation.status;
[self.navigationController pushViewController:destinationController animated:YES];
总而言之,您的方法可能如下所示:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
MyAnnotation *annotation = view.annotation;
DetailViewController *detail = [[DetailViewController alloc] initWithNibName:nil
bundle:nil];
detail.status = annotation.status;
detail.company = annotation.company;
[self.navigationController pushViewController:detail animated:YES];
}
然后在细节视图控制器中设置UILabel 文本:
- (void)viewDidLoad
{
[super viewDidLoad];
self.statusTextField.text = self.status;
self.companyTextField.text = self.company;
}
更新说明MyAnnotation的创建:
您始终可以选择创建自定义类。这可能是MyAnnotation.h 的示例:
#import <MapKit/MapKit.h>
@interface MyAnnotation : MKPointAnnotation
@property (strong, nonatomic) NSString *status;
@property (strong, nonatomic) NSString *company;
@end
然后在您的地图视图控制器中导入:#import "MyAnnotation.h"
并使用MyAnnotation 而不是MKPointAnnotation:
// create the annotation
newAnnotation = [[MyAnnotation alloc] init];
newAnnotation.title = dictionary[@"applicant"];
newAnnotation.subtitle = dictionary[@"company"];
newAnnotation.status = dictionary[@"status"];
newAnnotation.company = dictionary[@"company"];
newAnnotation.coordinate = location;
[newAnnotations addObject:newAnnotation];