【问题标题】:iOS: show json info on detailView when click on annotationiOS:单击注释时在 detailView 上显示 json 信息
【发布时间】:2014-07-21 22:24:18
【问题描述】:

我有一个包含一些信息的 json 文件我想在地图上显示这些信息,我可以在地图上显示 Long 和 Lat,当你点击 Annotation 我有一个按钮我想在详细视图中显示我的 json 的详细信息,我的问题是我不知道如何根据单击的注释在详细视图上显示/发送信息,

这是我将如何加载我的 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];
}

在我的日志中,我打印了正确的状态,但在我的详细视图控制器中,我打印了 null

- (void)viewDidLoad
{
[super viewDidLoad];

self.status.text = _status.text;
    NSLog(@"Status %@ ",_status );
    NSLog(@"Status is %@ ",self.status.text);

}

打印状态为空 // // // 状态为空

【问题讨论】:

    标签: ios objective-c json mapkit mapkitannotation


    【解决方案1】:

    您可以从提供的MKAnnotationView 访问数据

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

    view 对象有一个annotation 属性,它将为您提供一个采用MKAnnotation 协议的对象。这可能是您已经拥有的MKPointAnnotation,如果只有titlesubtitle 就可以。但是你也可以定义一个自定义注解类来保存statuscompany

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

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

    一旦你有了你需要的数据并且你想将它传递给 DetailViewController,我建议查看this SO answerOle 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];
    

    【讨论】:

    • 感谢您的回复,请您写完整的答案,我的意思是这个自定义部分 MyAnnotation *annotation = view.annotation; annotation.status ...基于我的代码,在此先感谢
    • 再次感谢我还有一个问题,我在我的详细视图上添加了一个标签并尝试使用状态但我的标签为空,你知道这里有什么问题吗?我在问题中添加了编辑部分
    • 我不肯定,但我认为您的空标签问题应该出现在一个新的 SO 问题中,否则这个问题会开始偏离原始问题。
    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2012-12-20
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    • 2014-05-25
    相关资源
    最近更新 更多