iOS:单击注释时在详细 View 上显示 json 信息

标签 ios objective-c json mapkit mapkitannotation

我有一个包含一些信息的 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,如果只是一个 titlesubtitle 就可以了。但您也可以定义一个自定义注释类,它保存在 statuscompany 上:

MyAnnotation *annotation = view.annotation; 
// annotation.status
// annotation.company

您必须创建一个 MyAnnotation 实例并将数据插入到您当前正在创建 newAnnotation 的位置。

至于一旦您拥有所需的数据并想将其传递给 DetailViewController,我建议查看 this SO answerOle 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/

相关文章:

json - 如何从 Auto Scaling 组获取私有(private) IP 地址

javascript - 使用javascript转义字符串中的特定字符

ios - class_copyIvarList 泄漏

ios - 在 64 种不同的可用概率中切换的最佳方式是什么

objective-c - 星号有时在变量类型上,有时在变量名上。为什么?

objective-c - 打印 NSDocument

objective-c - iOS - 在没有 xib 的项目中创建 xib

ios - SwiftyJSON 不适用于 socket.io

ios - 我有一个 UIAlertView 反复显示找不到源的问题

javascript - Google Analytics 中的交叉自定义变量集