Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 如何在添加的XIB视图(注释)中捕捉点击按钮_Ios_Swift_Mapkit - Fatal编程技术网

Ios 如何在添加的XIB视图(注释)中捕捉点击按钮

Ios 如何在添加的XIB视图(注释)中捕捉点击按钮,ios,swift,mapkit,Ios,Swift,Mapkit,1) 我有带地图工具包的ViewController 1.1)我在地图上添加了一些pin class ViewController: UIViewController, MKMapViewDelegate 2) 我为自定义管脚标注和注释编写了新类 class CustomPointAnnotation: MKPointAnnotation { class CustomCalloutView: UIView { 3) 我已经为我的自定义pin标注创建了.xib 4) 我在.xib中创建了这

1) 我有带地图工具包的ViewController

1.1)我在地图上添加了一些pin

class ViewController: UIViewController, MKMapViewDelegate
2) 我为自定义管脚标注和注释编写了新类

class CustomPointAnnotation: MKPointAnnotation {

class CustomCalloutView: UIView {
3) 我已经为我的自定义pin标注创建了.xib

4) 我在.xib中创建了这个按钮,例如,这个按钮必须执行某些操作

    @IBAction func clickTest(sender: AnyObject) {
    print("aaaaa")
}
5) 这个按钮坏了

通常是什么做法?我想让我的按钮工作。 按钮为蓝色:

在Github上可以看到的所有项目:
谢谢你,抱歉用英语说)

你要做的就是将按钮从.xib引用到相关的.swift文件中

在ViewController中,您可以执行以下操作:

let button:UIButton!

[...]

button.targetForAction("tappedButton:", withSender: self)

func tappedButton() {
    print("Taped !")
}

对于目标C:

更新

斯威夫特

为自定义调出的MKAnnotationView创建子类,并重写hitTest:和pointInside:方法

import UIKit
import MapKit

class CustomCalloutView: MKAnnotationView {

@IBOutlet weak var name: UILabel!

@IBAction func goButton(sender: AnyObject) {

    print("button clicked sucessfully")
}

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
    let hitView = super.hitTest(point, withEvent: event)
    if hitView != nil {
        superview?.bringSubviewToFront(self)
    }
    return hitView
}

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
    let rect = self.bounds
    var isInside = CGRectContainsPoint(rect, point)
    if !isInside {
        for view in subviews {
            isInside = CGRectContainsPoint(view.frame, point)
            if isInside {
                break
            }
        }
    }
    return isInside
}
}

谢谢你,我对你说的话做了解释,但得到的错误是:对不起我的错。您必须将xib文件作为子视图加载。看这里:如果
子视图
是加载的xib文件的名称,则从这里开始
subView.button.targetForAction…
Yes,我已加载:
code
let customView=(NSBundle.mainBundle().loadNibNamed(“ViewCallout”,所有者:self,选项:nil))[0]为!自定义视图
code
但我无法最终理解,您能在github上查看我的项目吗?让calloutViewFrame=customView.frame;您必须将此代码放入viewController中,但.xib文件需要它自己的.swift文件。看这里,对不起,您的解决方案是Objective-C,我使用Swift。我有自定义标注,我可以将变量发送到我的.xib,但无法创建工作按钮
//init XIB
让customView=(NSBundle.mainBundle().loadNibNamed(“ViewCallout”,所有者:self,选项:nil))[0]作为!自定义视图
让calloutViewFrame=customView.frame
让cpa=view.annotation为!CustomPointAnnotation
cpa.name=“Company name”
customView.name.text=cpa.name
view.addSubview(customView)
您必须为CustomCallout视图(现在是从UIView中进行子类化)创建MKAnnotationView子类,添加视图、按钮、,标签并覆盖hitTest:和pointInside:方法来实现按钮单击。你是对的,我觉得这是正确的方法。不要告诉我在哪里可以看到此重写方法的手册??(hitTest和pointInside)