gpt4 book ai didi

ios - 将参数从 pin 传递到其他 View

转载 作者:行者123 更新时间:2023-12-02 03:10:10 25 4
gpt4 key购买 nike

我在 map 上放置的 MKPointAnnotation 点出现问题。我会解释一下。

场景:

从服务器读取 JSON -> 在 map 上放置图钉 -> 单击图钉时,然后打开一个新 View ,并将参数传递到该 View 。该参数必须绑定(bind)到引脚。

到目前为止我的代码:

JSON(仅用于示例)

{"places":[{"lat":"30.03","lng":"40.31","id":"1"}]}

读取 JSON 并向 map 添加点:

NSString *markersJSON=@"http://test.com/json.php";
NSURLRequest *requestUsername = [NSURLRequest requestWithURL:[NSURL URLWithString:markersJSON]];
NSData *response = [NSURLConnection sendSynchronousRequest:requestUsername
returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response
options:0 error:&jsonParsingError];
markerArray = [json objectForKey:@"places"];

for (NSDictionary *jsonObj in markerArray ){
latJSON=[jsonObj objectForKey:@"lat"];
lngJSON=[jsonObj objectForKey:@"lng"];
alertID=[jsonObj objectForKey:@"id"];
CLLocationCoordinate2D myCoordinate = {[latJSON doubleValue], [lngJSON doubleValue]};
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = myCoordinate;
point.title=[jsonObj objectForKey:@"title"];
point.subtitle=[jsonObj objectForKey:@"description"];
[self.mapView addAnnotation:point];
}

单击注释时,使用与引脚对应的 JSON 中的变量“id”打开新 View 。

- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
{
ViewController *yourViewController = (ViewController *)[navStoryBoard instantiateViewControllerWithIdentifier:@"details_alert"];

**//STUCKED HERE**

[self.navigationController pushViewController:yourViewController animated:YES];

}

问题是我找不到将 id 从 JSON 绑定(bind)到相应 pin 的方法,因此每次用户触摸 pin 时,我都会知道 id=1 的 pin 是触摸,然后进行其他操作。例如,打开包含从数据库获取的数据的详细 View 。

我希望你能理解我想做什么。

谢谢!

编辑:快到了。

//Annotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface Annotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *placeId;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,readonly,copy) NSString *title;
@property (nonatomic,readonly,copy) NSString *subtitle;
@property (nonatomic) NSString *placeId;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description placeId:(NSString *)placeId;

@end

//Annotation.m

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize placeId;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description placeId:(NSString *)placeIda;
{
self = [super init];
if (self != nil) {
coordinate = location;
title = placeName;
subtitle = description;
placeId=placeIda;

}
return self;
}

- (void)dealloc {

}
@end


Add pins from JSON loop:

for (NSDictionary *jsonObj in markerArray ){
latJSON=[jsonObj objectForKey:@"lat"];
lngJSON=[jsonObj objectForKey:@"lng"];
alertID=[jsonObj objectForKey:@"id"];
CLLocationCoordinate2D myCoordinate = {[latJSON doubleValue], [lngJSON doubleValue]};

Annotation *pin = [[Annotation alloc] initWithCoordinates:myCoordinate placeName:[jsonObj objectForKey:@"title"] description:[jsonObj objectForKey:@"description"] placeId:alertID];
[self.mapView addAnnotation:pin];
}

Touch pin and pass pin ID to other view

- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
{
id <MKAnnotation> annotation = [view annotation];
CLLocationCoordinate2D centerCoordinate =[annotation coordinate ];

ViewController *yourViewController = (ViewController *)[navStoryBoard instantiateViewControllerWithIdentifier:@"details_alert"];

//////
Annotation *mysc=view.annotation;
[yourViewController setValue:[NSString stringWithFormat:@"%lu",(unsigned long)mysc.placeId] forKey:@"sendAlertID"];
//////

[self.navigationController pushViewController:yourViewController animated:YES];

}

以上所有工作都有效。但是传递给 viewController 的 mysc.placeId 完全错误。例如,对于 id=1(来自 JSON)的 pin,sendAlertID(来自 yourViewController 的变量)的 NSLog 是 245513072!!!

最佳答案

- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
{
ViewController *yourViewController = (ViewController *)[navStoryBoard instantiateViewControllerWithIdentifier:@"details_alert"];

**//STUCKED HERE**

[self.navigationController pushViewController:yourViewController animated:YES];

}

所以:

只需创建符合 MKAnnotation 的您自己的对象即可协议(protocol)。然后您可以:

MyAnnotationObject *object = (MyAnnotationObject *)view.annotation;
NSString *jsonId = object.id;

而不是:

 MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = myCoordinate;
point.title=[jsonObj objectForKey:@"title"];
point.subtitle=[jsonObj objectForKey:@"description"];

您将使用自己的类:

MyAnnotationObject *annotation = [[MyAnnotationObject alloc] initWithCoordinates:coordinates title:title subtitle:subtitle];

你可以看到这个simple example on how to do it

<小时/>

你只需要两件事:

  1. 遵守MKAnnotation协议(protocol):@interface MyAnnotationObject :NSObject <MKAnnotation>
  2. 创建@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

关于ios - 将参数从 pin 传递到其他 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21092085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com