映射自定义视图注释

时间:2015-07-13 11:09:50

标签: ios swift mkannotation mkannotationview

我正在尝试创建一个自定义注释,它有两个按钮。我还用两个按钮实现了自定义注释视图。但是,我的问题是。当我点击按钮时,我的按钮操作不会被调用。所以请帮助我。怎么可能。我正在使用以下代码创建'XIB'并调用所需的方法。

首先你看这个图像网址。

1:http://i.stack.imgur.com/fOT7m.png

此代码用于创建自定义视图。

import UIKit

class YSCustomCallOutView: UIView {

@IBOutlet var ysSeeDirections_Button: UIButton!
@IBOutlet var ysSeeListingDetail_Button: UIButton!
var view:UIView!

override init(frame: CGRect) {
    super.init(frame: frame)
    setUp()
}

required init(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)
    setUp()
}

func setUp(){

    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
    addSubview(view)
}

func loadViewFromNib() -> UIView{

    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "YSCustomCallOutView", bundle: bundle)
    let view = nib.instantiateWithOwner(self, options: nil)[0] as UIView
    return view
}

}

这是必需的方法,我已经调用它。

var calloutView:YSCustomCallOutView!
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!){


    if(!view.annotation .isKindOfClass(MKUserLocation)){

    }
    calloutView =  YSCustomCallOutView(frame: CGRectMake(-42,-91,173,91))
    calloutView.ysSeeListingDetail_Button.addTarget(self, action: Selector("checkin:"), forControlEvents: UIControlEvents.TouchUpInside)
    calloutView.ysSeeDirections_Button.addTarget(self, action: Selector("checkout:"), forControlEvents: UIControlEvents.TouchUpInside)

    view.addSubview(calloutView)

}

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

        calloutView.removeFromSuperview()
}

func checkin(sender:UIButton){

    println("Hello")
}

func checkout(sender:UIButton){

    println("Friend")
}

0 个答案:

没有答案