【问题标题】:How to make an action for a map annotated button in swift如何快速为带有地图注释的按钮执行操作
【发布时间】:2016-09-27 07:33:16
【问题描述】:

我对 swift 编程很陌生,我想制作一个带有 MKMapview 的应用程序,该应用程序包含多个注释,带有与其他 UIViewControlers 连接的按钮。我是否可以像您通常使用常规 UIButton 一样创建一个动作?如果没有,我该如何选择其他 UIViewControllers?

【问题讨论】:

  • 你不能只为进入函数的按钮添加一个目标吗?或者类似地将按钮连接到 IBAction?我以前没有使用过 MKMapview,但我无法想象它会有很大的不同

标签: swift uibutton annotations mkmapview segue


【解决方案1】:

您需要制作一个自定义注释视图:

 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    // add here a button
    var button = UIButton.buttonWithType(.DetailDisclosure) as UIButton // button with info sign
    yourView.rightCalloutAccessoryView = button
}

然后检查按钮何时被按下:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if control == view.rightCalloutAccessoryView{
        print(view.annotation.title) // your annotation's title
        //Perform a segue here to navigate to another viewcontroller
        self.performSegueWithIdentifiert("yourSegue")
    }
}

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多