ios - 按钮事件不会在 MAP 上的 CustomView 内部调用?

标签 ios swift dictionary

我正在将 map 注释集成到我的应用程序中。我在 map 上添加注释如下。 MAP上的所有标注都添加成功。

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if (annotation is MKUserLocation) {
            return nil
        }

        let reuseId = "CustomAnnotation"
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        annotationView.image = UIImage(named:"MapTemp")
        annotationView.canShowCallout = false

        return annotationView
    }

我在点击 map 注释时显示一个 customView。单击注释时,将调用其委托(delegate),其中将自定义 View 添加到 map 上,如下面的代码所示-

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
    {
        if (self.viewCustom != nil)
        {
            self.viewCustom.removeFromSuperview()
        }

        let nib:Array = NSBundle.mainBundle().loadNibNamed("View123", owner: self, options: nil)
        self.viewCustom = nib[0] as? UIView

        println(self.viewCustom.viewWithTag(100))


        if let buttonFB = self.viewCustom.viewWithTag(100) as? UIButton {

            buttonFB.addTarget(self, action:"clickFacebook:", forControlEvents: UIControlEvents.TouchUpInside)
        }

        self.addBounceAnimationToView(viewCustom)

        var hitPoint : CGPoint = view.convertPoint(CGPointZero, toView: viewCustom)

        self.viewCustom!.frame = CGRectMake(-100,-200, 250, 200)

        view.addSubview(self.viewCustom!)
        self.view.bringSubviewToFront(self.viewCustom)

    }

在自定义 View 中,我有一个按钮 buttonFB 指定了标签值和事件 clickFacebook

@IBAction func clickFacebook(sender: AnyObject)
 {
     //Face
 }

但是这个按钮的点击事件不会触发,只有 Mapview 的委托(delegate) didDeselectAnnotationView 总是被调用(下面的方法被调用)。

func mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKAnnotationView!)
    {
      //
    }

我想让按钮的事件触发。我该怎么做。

enter image description here

darkgray Image是MapView的Annotation,黄色部分是根据图片的button。

请帮帮我。

最佳答案

didSelectAnnotationView: 方法中,您将 self.view 放在前面,因此 self.view 将位于包含 map 的顶部,因此这些事件将在 self view 上运行。您的 self.viewCustom 位于 self.view 下方,当您触摸 self.view 时会处理触摸。

您需要将您的 self.viewCustom 置于 self.view 的顶部,然后它才能进行触摸。

你需要改变声明-

self.viewCustom.bringSubviewToFront(self.view)

self.view.bringSubviewToFront(self.viewCustom)

关于ios - 按钮事件不会在 MAP 上的 CustomView 内部调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435134/

相关文章:

python - 使用 pd.json_normalize 展平字典

ios - Cocos2d Sprite 无法在设备上运行

ios - 合并 AppDelegate_iPhone 和 AppDelegate_iPad

ios - 2 行(多行)长文本选择器

ios - 用户上次事件

ios - 如何将数据 CollectionViewCell 数组追加到 UIViewController 数组

c# - NullReferenceException 添加到字典中 - 非常确定没有什么是空的

ios - 横向 iPhone X 上的全屏 UICollectionViewCell 问题

ios - 为什么堆栈 View 会将等间距的水平按钮更改为框中等间距的按钮?

ios - 使用谓词 iOS SDK 从字典数组中过滤数据