gpt4 book ai didi

ios - 传递 Parse PFobject objectID 映射注解 View 按钮标题

转载 作者:行者123 更新时间:2023-11-29 12:56:50 24 4
gpt4 key购买 nike

我有一个注释 View 设置,它向 rightCalloutAccessoryView 添加了一个 UIButton。我想要做的是将按钮的标题设置为注释表示的解析对象的 objectID。

我当前的结果是每个注释都获取查询中第一个对象的 objectID。我在这里做错了什么?

查看下面的代码,我发现每次在 map 上放置图钉时,并没有设置 restaurantID。在我看来,我把它放在了正确的位置,因为图钉的标题和副标题已正确更新。

这是我所做的:

1.) 在我的 mainViewController.h 中,我声明了一个字符串来保存 PFobject 的 objectID:

@property (strong, nonatomic) NSString *restaurantID;

2.) 在我的 mainViewController.m 中,我合成了变量

@synthesize restaurantID;

3.) 在我的 PFquery 中,我将此 restaurantID 设置为 PFobject 的 objectID (restaurantID = [object objectId];):

 for (PFObject *object in objects) {
PFGeoPoint *point = [[object objectForKey:@"geoLocation"] init];

CLLocationCoordinate2D spot;
spot.latitude = point.latitude;
spot.longitude = point.longitude;
MKPointAnnotation *foodPoint = [[MKPointAnnotation alloc] init];
foodPoint.coordinate = (spot);
restaurantID = [object objectId];
foodPoint.title = [object objectForKey:@"restaurantName"];
foodPoint.subtitle = [object objectForKey:@"cuisineType"];
leftIcon = [object objectForKey:@"restaurantImage"];
[leftIcon getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
image = [UIImage imageWithData:data];
// image can now be set on a UIImageView
}
}];
[self.mainMap addAnnotation:foodPoint];
}

然后在我的注释 View 中,我将按钮的标题设置为 restaurantID:

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:restaurantID forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(buttonMethod:)
forControlEvents:UIControlEventTouchUpInside];

在我的 buttonMethod 中:我有一个 NSLog 来显示按下的按钮的标题。当我点击不同的引脚时,该值保持不变。它会获取一个 Parse 对象的 objectID,但不会为查询中的每个对象获取新的 objectID。

有什么想法吗?

最佳答案

viewForAnnotation 委托(delegate)不一定在您调用addAnnotation 后立即调用它不一定只调用一次对于每个注释。

本地图 View 想要显示注释时,它会调用委托(delegate)方法。这可能会比您添加时稍晚发生,或者如果用户平移或缩放 map 并且注释再次出现在屏幕上,则可能会再次发生相同的注释。

因此,您不能像现在这样使用 View Controller 级别的 restaurantID 变量(假设它只是根据 map View 正在调用的注释设置的 viewForAnnotation )。本地图 View 调用 viewForAnnotation 时,restaurantID 被设置为某个与 map View 当前获取 View 的注释无关的值。


相反,您应该将 objectId(甚至整个 object)单独放在每个注释中。

现在,由于您正在使用 MKPointAnnotation 类,您可以设置的唯一属性是 coordinatetitle副标题

您需要继承 MKPointAnnotation 或创建您自己的自定义类来实现 MKAnnotation 协议(protocol)并向其添加 restaurantID 属性.

添加注解时设置该属性:

CustomAnnotationClass *foodPoint = [[CustomAnnotationClass alloc] init];
foodPoint.coordinate = (spot);
foodPoint.restaurantID = [object objectId]; //<--put objectId in foodPoint
foodPoint.title = [object objectForKey:@"restaurantName"];

然后在 viewForAnnotation 中,使用 annotation 参数获取与 map View 当前调用的注释相关的值:

//first make sure annotation is our custom type...
if ([annotation isKindOfClass:[CustomAnnotationClass class]])
{
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

//cast annotation parameter to our class so compiler understands...
CustomAnnotationClass *fp = (CustomAnnotationClass *)annotation;

//get restaurantID from the annotation parameter ("fp")...
[rightButton setTitle:fp.restaurantID forState:UIControlStateNormal];

...
}

还要确保正确处理注释 View 的重用。也就是说,如果 dequeue 返回一个 View ,则将其 annotation 属性更新为当前 View (例如,annotationView.annotation = annotation;)。如果不这样做, View 或标注可能再次显示不同注释的数据。


这应该可以解决按钮显示错误标题的问题。

但是,我假设您设置按钮的标题只是为了了解用户点击了哪个注释。这会“起作用”,但有点笨拙,因为标题 (restaurantID) 将出现在披露图标的右侧(尽管它可能在标注中不可见)。

我强烈建议使用 map View 的 calloutAccessoryControlTapped 委托(delegate)方法或 map View 的 selectedAnnotations 属性来代替该方法。有关示例,请参见以下问题:

这样,您可以获得对注释对象本身的引用,并从中获取 restaurantID(与 viewForAnnotation 中的方式相同)。

关于ios - 传递 Parse PFobject objectID 映射注解 View 按钮标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863536/

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