【问题标题】:Change color of pin on map after pressing button on another screen在另一个屏幕上按下按钮后更改地图上图钉的颜色
【发布时间】:2013-05-21 19:28:13
【问题描述】:

我需要找到一种方法来在加载地图时通过从另一个屏幕按下按钮来更改图钉颜色。

例如,原始图钉都是红色的,但是当我进入一个页面并按下地图按钮时,它必须将我引导到地图视图,并且该位置的坐标必须用绿色图钉标记。

我已经设置好了地图,并设置了将大头针全部设置为红色。

感谢任何帮助。

所以,简单回顾一下: 带有红色图钉的页面 --> 点击图钉,点击注释(另一个视图打开)--> 内部视图,有一个按钮(例如@“更改图钉颜色”),点击按钮 --> 带有绿色图的页面销打开。 (请看上面的图片示例。)

【问题讨论】:

    标签: iphone ios xcode annotations mkmapview


    【解决方案1】:

    您应该做的是更改代表该引脚的数据中的某些内容。注释。当您切换回地图时,注释将被重新绘制,您的 viewForAnnotation 方法将被调用,此时您检查属性并绘制正确的颜色。

    【讨论】:

    • 但是我如何让这个在不同的视图之间改变,因为我有很多这些类型的视图需要调出不同的颜色,然后在返回按钮时返回原始颜色被按下了吗?
    • 您需要将视图控制器推送到导航控制器堆栈上。如果你不知道这意味着什么,你需要做一些研究。排序后,为新视图控制器提供一个属性,父视图控制器可以在子视图控制器关闭后读取该属性。然后读取该属性并根据需要更改您的注释。
    • 是否有类似的操作: if (previous screen name: @"") { } 因为这对我来说最容易。
    【解决方案2】:

    您为什么不直接声明一个新的构造函数来设置您选择的颜色?

    // ViewControllerB .h file
    @interface ViewControllerB
    {
        UIColor *pinColor;
    }
    
    -(id)initWithPinColor:(UIColor *)chosenPinColor;
    
    ...
    
    // ViewControllerB .m file
    -(id)initWithPinColor:(UIColor *)chosenPinColor
    {
        self = [super init];
    
        if(self)
        {
            pinColor = chosenPinColor;
        }
    
        return self;
    }
    
    ...
    
    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        if([annotation isKindOfClass:[MKUserLocation class]])
        {
            return nil;
        }
    
        static NSString *annotationViewID = @"annotationViewID";
    
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
    
        if(!annotationView)
        {
            annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewID] autorelease];
        }
    
        // this will use the pinColor stored in the constructor
        annotationView.pinColor = pinColor;        
        annotationView.canShowCallout = YES;
        annotationView.animatesDrop = YES;
    
        annotationView.annotation = annotation;
    
        return annotationView;
    }
    

    然后你可以在 ViewControllerA 中简单地这样做:

    // ViewControllerA .m file
    #import "ViewControllerB.h"
    
    ...
    
    -(void)showMapWithPinColor:(id)sender
    {
        ViewControllerB *vc = [[ViewControllerB alloc] initWithPinColor:[UIColor green]];
    
        [self.navigationController pushViewController:vc animated:YES];
    }
    

    【讨论】:

    • 我没有使用导航控制器,有什么办法可以解决吗?
    • 如果您使用自定义 UIView 类,您可以在其中创建一个协议,声明一个类型为 id 委托的属性,并在您的协议中创建一个非可选方法(例如-(void)changePinColor;) 您的原始视图控制器必须实现。在您的“更改颜色”按钮点击回调方法中,您可以调用 [self.delegate changePinColor];然后在您的原始视图控制器中,我假设您存储了最后点击的引脚注释视图,所以只需使用类似 lastSelectedPin.pinColor = [UIColor PurpleColor];在实现的协议方法中。
    • 另一种方法是在您的自定义 UIView 类中声明一个属性 MKAnnotationView 并将原始视图控制器中的原始 lastSelectedPin 分配给它。然后,当您在自定义类中执行 buttonTapped 方法时,您只需转到 originalPin.pinColor = [UIColor PurpleColor];
    • 我还是很困惑...有没有可以在从特定 xib 打开地图时更改地图设置的功能?
    猜你喜欢
    • 2015-08-06
    • 2015-02-23
    • 2016-02-03
    • 2023-02-21
    • 2021-05-02
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2015-10-01
    相关资源
    最近更新 更多