出于某种原因,我的标注 View 中显示了两个信息按钮。我有 3 种不同的图钉颜色,它们从 3 个不同的来源提取信息。我需要将每个引脚颜色的每个按钮推送到基于该注释类的单独 View Controller 。到目前为止,我有以下代码用于我的 3 种不同的引脚颜色:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[annotation class]]) {
MKPinAnnotationView *view = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (view == nil) {
view = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:identifier];
} else {
view.annotation = annotation;
}
if([[(Annotation*)annotation phoneNumber] isEqualToString:@"0"]){
view.enabled = YES;
view.canShowCallout = YES;
view.pinColor = MKPinAnnotationColorPurple;
// Create a UIButton object to add on the
self.leftBtn = [UIButton buttonWithType:UIButtonTypeInfoDark];
[self.leftBtn setTitle:annotation.title forState:UIControlStateNormal];
[view setLeftCalloutAccessoryView:self.leftBtn];
}else{
view.enabled = YES;
view.canShowCallout = YES;
// Create a UIButton object to add on the
self.rightBtn = [UIButton buttonWithType:UIButtonTypeInfoDark];
[self.rightBtn setTitle:annotation.title forState:UIControlStateNormal];
[view setRightCalloutAccessoryView:self.rightBtn];
}
if ([[(Annotation*)annotation phoneNumber] isEqualToString:@"1"]){
view.enabled = YES;
view.canShowCallout = YES;
view.pinColor = MKPinAnnotationColorGreen;
// Create a UIButton object to add on the
self.leftBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[self.leftBtn setTitle:annotation.title forState:UIControlStateNormal];
[view setLeftCalloutAccessoryView:self.leftBtn];
}
return view;
}
return nil;
}
对于我的附件点击方法,我有以下内容:
- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
if(view.leftCalloutAccessoryView){
UserAnnotation *annotate = view.annotation;
BuydealsViewController *dealsView=[[BuydealsViewController alloc]initWithNibName:@"DealDetailsViewViewController" bundle:[NSBundle mainBundle]];
dealsView.urlString = annotate.url;
[self.navigationController pushViewController:dealsView animated:YES];
}
if(view.rightCalloutAccessoryView){
MapDealViewController *mapDeals = [[MapDealViewController alloc] init];
Annotation *annView = view.annotation;
mapDeals.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mapDeals.phoneNumber = annView.phoneNumber;
mapDeals.address = annView.subtitle;
mapDeals.title = annView.title;
mapDeals.description = annView.description;
mapDeals.value = annView.value1;
mapDeals.discountPercent = annView.discountPercent;
mapDeals.price = annView.price;
mapDeals.header = annView.header;
mapDeals.imageUrl = annView.imageUrl;
mapDeals.url = annView.url;
mapDeals.dealy = annView.dealy;
self.navigationController.navigationBarHidden = NO;
[self.navigationController pushViewController:mapDeals animated:YES];
}
}
有什么方法可以让它们都成为右键但单击以分隔 View ?
最佳答案
首先,目前这可能不会引起任何问题,但请务必指出。
viewForAnnotation
中的这一行:
if ([annotation isKindOfClass:[annotation class]])
看起来不对,因为当然 annotation
将与本身 属于同一类。
这将始终返回 YES
。
您可能打算用大写的 A
编写 Annotation class
,这是类名(annotation
和小写的 a
指当前注解实例参数):
if ([annotation isKindOfClass:[Annotation class]])
关于 “出于某种原因,我的标注 View 中显示了两个信息按钮。”:
在 viewForAnnotation
中查看当前逻辑的这个简化版本:
if (phoneNumber is "0")
{
...
[view setLeftCalloutAccessoryView:self.leftBtn];
}
else
{
....
[view setRightCalloutAccessoryView:self.rightBtn];
}
if (phoneNumber is "1")
{
....
[view setLeftCalloutAccessoryView:self.leftBtn];
}
请注意,如果 phoneNumber 为“1”:
- View 的
rightCalloutAccessoryView
在else
中设置,因为“1”不是“0”, - 并且 View 的
leftCalloutAccessoryView
在第二个if
中设置(因为它是“1”)。
accessory views的设置逻辑需要根据自己的需要进行调整。
请注意,当给定注释不应该具有该附件(以及其他时注释可能)。这是因为如果注释 View 被重新使用,它的附属 View 可能已经根据它之前使用的注释进行了设置。所以你可以这样做:
if (phoneNumber is "0")
{
...
view.leftCalloutAccessoryView = nil;
view.rightCalloutAccessoryView = [UIButton buttonWithType...];
}
else if (phoneNumber is "1")
{
...
view.leftCalloutAccessoryView = [UIButton buttonWithType...];
view.rightCalloutAccessoryView = nil;
}
else
{
//some default, unexpected case handling...
...
view.leftCalloutAccessoryView = nil;
view.rightCalloutAccessoryView = nil;
}
此外,不需要将 leftBtn
和 rightBtn
作为属性,这可能会导致混淆或其他问题。只需将它们声明为局部变量即可。
关于 “有什么方法可以使它们都是右键按钮,但单击以分隔 View ?”:
是的。 viewForAnnotation
中已有执行此操作的代码:
- 您可以检查
annotation
对象的class
(但要正确使用 class 名称而不是 instance 变量)。 - 您可以将
注释
转换到您的自定义类并检查自定义属性值(如phoneNumber
)。
关于ios - 2 出现在标注上的 AccessoryItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19850107/