我有一个包含一些信息的 json 文件我想在 map 上显示这些信息,我可以在 map 上显示经度和纬度,当您单击注释时我有一个按钮我想在详细 View 中显示我的 json 的详细信息,我的问题是我不知道如何根据点击的注释显示/发送详细 View 的信息,
这是我加载 detailViwe 的方式
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
DetailViewController *detail = [[DetailViewController alloc] initWithNibName:nil
bundle:nil];
// my question is here In my detail view I have label like status, company, how to add json info here
detail.status.text = ??!!!
detail.company.text = ??!!!
[self.navigationController pushViewController:detail animated:YES];
}
在我的日志中,我打印了正确的状态,但在我的详细 View Controller 中,我打印了 null
- (void)viewDidLoad
{
[super viewDidLoad];
self.status.text = _status.text;
NSLog(@"Status %@ ",_status );
NSLog(@"Status is %@ ",self.status.text);
}
打印状态为空 //////状态为空
最佳答案
您可以从中提供的MKAnnotationView
访问数据
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
view
对象有一个annotation
属性,它将为您提供一个采用MKAnnotation
协议(protocol)的对象。这可能是您已经拥有的 MKPointAnnotation
,如果只是一个 title
和 subtitle
就可以了。但您也可以定义一个自定义注释类,它保存在 status
和 company
上:
MyAnnotation *annotation = view.annotation;
// annotation.status
// annotation.company
您必须创建一个 MyAnnotation
实例并将数据插入到您当前正在创建 newAnnotation
的位置。
至于一旦您拥有所需的数据并想将其传递给 DetailViewController,我建议查看 this SO answer或 Ole Begemann's tips here .简而言之,您可以为细节 View Controller 创建一个公共(public)属性,然后执行如下操作:
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];
}
然后在详细 View Controller 中设置 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
然后在您的 map View Controller 中导入:#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];
关于iOS:单击注释时在详细 View 上显示 json 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24875845/