iOS:单击注释时在detailView上显示json信息

时间:2014-07-21 22:24:19

标签: ios objective-c json mapkit mapkitannotation

我有一个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];
}

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

- (void)viewDidLoad
{
[super viewDidLoad];

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

}

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

1 个答案:

答案 0 :(得分:1)

您可以访问

中提供的MKAnnotationView中的数据

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

view对象具有annotation属性,该属性将为您提供采用MKAnnotation协议的对象。如果您只有MKPointAnnotationtitle,那么这可能是subtitle。但您也可以定义一个保留在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];