将按钮添加到 MKPointAnnotation
Add button to MKPointAnnotation
尝试在注释中添加按钮时遇到问题。
在问这个问题之前,我在以下页面上搜索了答案:
, Adding a button to MKPointAnnotation?
等等
但是都帮不了我。
这是试图做的事情:
var annotation1 = MKPointAnnotation()
annotation1.setCoordinate(locationKamer1)
annotation1.title = "Title1"
annotation1.subtitle = "Subtitle1"
// here i want to add a button which has a segue to another page.
mapView.addAnnotation(annotation1)
不知道我尝试做的是否不起作用。
我是第一次尝试 swift。
希望有人能帮助我:)
提前致谢!
你在第一个 link 中的回答基本上是正确的,尽管它需要更新 Swift 2.
最重要的是,在回答您的问题时,您在创建注释时没有添加按钮。在 viewForAnnotation
.
中创建其注释视图时创建按钮
所以,你应该:
将视图控制器设置为地图视图的代理。
使视图控制器符合地图视图委托协议,例如:
class ViewController: UIViewController, MKMapViewDelegate { ... }
通过 control 从带有地图的场景上方的视图控制器图标拖动,将视图控制器(不是按钮)的 segue 添加到下一个场景查看下一场景:
然后select那个segue,然后给它一个故事板标识符(在我的例子中是"NextScene",尽管你应该使用一个更具描述性的名字):
实施viewForAnnotation
将按钮添加为右侧附件。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var view = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier)
if view == nil {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
view?.canShowCallout = true
view?.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
} else {
view?.annotation = annotation
}
return view
}
实施 calloutAccessoryControlTapped
其中 (a) 捕获点击的注释;和(b)启动segue:
var selectedAnnotation: MKPointAnnotation!
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
selectedAnnotation = view.annotation as? MKPointAnnotation
performSegueWithIdentifier("NextScene", sender: self)
}
}
实现一个 prepareForSegue
来传递必要的信息(大概你想传递注释,因此在第二个视图控制器中有一个 annotation
属性 ).
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let destination = segue.destinationViewController as? SecondViewController {
destination.annotation = selectedAnnotation
}
}
现在您可以像以前一样创建注释了:
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "Title1"
annotation.subtitle = "Subtitle1"
mapView.addAnnotation(annotation)
尝试在注释中添加按钮时遇到问题。
在问这个问题之前,我在以下页面上搜索了答案:
这是试图做的事情:
var annotation1 = MKPointAnnotation()
annotation1.setCoordinate(locationKamer1)
annotation1.title = "Title1"
annotation1.subtitle = "Subtitle1"
// here i want to add a button which has a segue to another page.
mapView.addAnnotation(annotation1)
不知道我尝试做的是否不起作用。 我是第一次尝试 swift。
希望有人能帮助我:)
提前致谢!
你在第一个 link 中的回答基本上是正确的,尽管它需要更新 Swift 2.
最重要的是,在回答您的问题时,您在创建注释时没有添加按钮。在 viewForAnnotation
.
所以,你应该:
将视图控制器设置为地图视图的代理。
使视图控制器符合地图视图委托协议,例如:
class ViewController: UIViewController, MKMapViewDelegate { ... }
通过 control 从带有地图的场景上方的视图控制器图标拖动,将视图控制器(不是按钮)的 segue 添加到下一个场景查看下一场景:
然后select那个segue,然后给它一个故事板标识符(在我的例子中是"NextScene",尽管你应该使用一个更具描述性的名字):
实施
viewForAnnotation
将按钮添加为右侧附件。func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { var view = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) if view == nil { view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) view?.canShowCallout = true view?.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) } else { view?.annotation = annotation } return view }
实施
calloutAccessoryControlTapped
其中 (a) 捕获点击的注释;和(b)启动segue:var selectedAnnotation: MKPointAnnotation! func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { if control == view.rightCalloutAccessoryView { selectedAnnotation = view.annotation as? MKPointAnnotation performSegueWithIdentifier("NextScene", sender: self) } }
实现一个
prepareForSegue
来传递必要的信息(大概你想传递注释,因此在第二个视图控制器中有一个annotation
属性 ).override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let destination = segue.destinationViewController as? SecondViewController { destination.annotation = selectedAnnotation } }
现在您可以像以前一样创建注释了:
let annotation = MKPointAnnotation() annotation.coordinate = coordinate annotation.title = "Title1" annotation.subtitle = "Subtitle1" mapView.addAnnotation(annotation)