Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 显示标注上的2个附件项_Ios_Objective C_Mapkit - Fatal编程技术网

Ios 显示标注上的2个附件项

Ios 显示标注上的2个附件项,ios,objective-c,mapkit,Ios,Objective C,Mapkit,出于某种原因,我的详图索引视图中显示了两个信息按钮。我有3种不同的pin颜色,它们从3个不同的来源获取信息。我需要根据注释类将每个针颜色的每个按钮推送到单独的视图控制器。到目前为止,我的3种不同pin颜色的代码如下: - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if([annotation isKindOfClass:

出于某种原因,我的详图索引视图中显示了两个信息按钮。我有3种不同的pin颜色,它们从3个不同的来源获取信息。我需要根据注释类将每个针颜色的每个按钮推送到单独的视图控制器。到目前为止,我的3种不同pin颜色的代码如下:

- (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;
}

有没有办法使它们都是正确的按钮,但单击以分离视图

首先,这目前可能不会造成任何问题,但需要指出的是。
注释视图中的这一行:

if ([annotation isKindOfClass:[annotation class]])
if (phoneNumber is "0") 
{
    ...
    [view setLeftCalloutAccessoryView:self.leftBtn];
}
else
{
    ....
    [view setRightCalloutAccessoryView:self.rightBtn];
}

if (phoneNumber is "1") 
{
    ....
    [view setLeftCalloutAccessoryView:self.leftBtn];
}
看起来是错误的,因为
注释
将与它本身是同一类。
这将始终返回
YES

您可能打算使用大写的
A
来编写
Annotation class
,A是类名(
Annotation
,小写的
A
表示当前的注释实例参数):


关于“出于某种原因,我的调用视图中显示了两个信息按钮”。:

在注释视图中查看当前逻辑的简化版本:

if ([annotation isKindOfClass:[annotation class]])
if (phoneNumber is "0") 
{
    ...
    [view setLeftCalloutAccessoryView:self.leftBtn];
}
else
{
    ....
    [view setRightCalloutAccessoryView:self.rightBtn];
}

if (phoneNumber is "1") 
{
    ....
    [view setLeftCalloutAccessoryView:self.leftBtn];
}
请注意,如果phoneNumber为“1”:

  • 视图的
    rightCalloutAccessoryView
    else
    中设置,因为“1”不是“0”
  • 视图的
    leftCalloutAccessoryView
    在第二个
    中设置(因为它是“1”)
需要根据需要调整设置附件视图的逻辑

请注意,当给定注释不应包含该附件时(以及当其他注释可能包含该附件时),应将视图的
leftCalloutAccessoryView
rightCalloutAccessoryView
显式设置为
nil
。这是因为,如果重新使用注释视图,则其附件视图可能已经基于以前使用的注释进行了设置。所以你可以这样做:

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
rightbn
作为属性,这可能会导致混淆或其他问题。只需将它们声明为局部变量


关于“是否有办法使它们都是正确的按钮,但单击以分离视图?”:

对。您在
viewForAnnotation
中已经有了执行以下操作的代码:

  • 您可以检查
    注释
    对象的
    (但请使用类名而不是实例变量正确执行)
  • 您可以向自定义类强制转换
    注释
    ,并检查自定义属性值(如
    电话号码

首先,这目前可能不会导致任何问题,但必须指出。
注释视图中的这一行:

if ([annotation isKindOfClass:[annotation class]])
if (phoneNumber is "0") 
{
    ...
    [view setLeftCalloutAccessoryView:self.leftBtn];
}
else
{
    ....
    [view setRightCalloutAccessoryView:self.rightBtn];
}

if (phoneNumber is "1") 
{
    ....
    [view setLeftCalloutAccessoryView:self.leftBtn];
}
看起来是错误的,因为
注释
将与它本身是同一类。
这将始终返回
YES

您可能打算使用大写的
A
来编写
Annotation class
,A
是类名(
Annotation
,小写的
A
表示当前的注释实例参数):


关于“出于某种原因,我的调用视图中显示了两个信息按钮”。:

在注释视图中查看当前逻辑的简化版本:

if ([annotation isKindOfClass:[annotation class]])
if (phoneNumber is "0") 
{
    ...
    [view setLeftCalloutAccessoryView:self.leftBtn];
}
else
{
    ....
    [view setRightCalloutAccessoryView:self.rightBtn];
}

if (phoneNumber is "1") 
{
    ....
    [view setLeftCalloutAccessoryView:self.leftBtn];
}
请注意,如果phoneNumber为“1”:

  • 视图的
    rightCalloutAccessoryView
    else
    中设置,因为“1”不是“0”
  • 视图的
    leftCalloutAccessoryView
    在第二个
    中设置(因为它是“1”)
需要根据需要调整设置附件视图的逻辑

请注意,当给定注释不应包含该附件时(以及当其他注释可能包含该附件时),应将视图的
leftCalloutAccessoryView
rightCalloutAccessoryView
显式设置为
nil
。这是因为,如果重新使用注释视图,则其附件视图可能已经基于以前使用的注释进行了设置。所以你可以这样做:

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
rightbn
作为属性,这可能会导致混淆或其他问题。只需将它们声明为局部变量


关于“是否有办法使它们都是正确的按钮,但单击以分离视图?”:

对。您在
viewForAnnotation
中已经有了执行以下操作的代码:

  • 您可以检查
    注释
    对象的
    (但请使用类名而不是实例变量正确执行)
  • 您可以向自定义类强制转换
    注释
    ,并检查自定义属性值(如
    电话号码

哇,太棒了!是的,我没有意识到我为注释设置了MKUserLocation条件,另一个是为注释类设置的。谢谢你,谢谢你!哇,太棒了!是的,我没有意识到我为注释设置了MKUserLocation条件,另一个是为注释类设置的。谢谢你,谢谢你!