如何使用标注按钮打开新的 uiviewcontroller。 (Swift)
How to use callout button to open new uiviewcontroller. (Swift)
例如,我在地图上放置了几个图钉,对应于不同的桥梁位置。单击这些图钉时,每个图钉都有自己的注释,显示它们的标题和副标题。我在这些注释中添加了一个信息按钮。但是我不知道如何打开一个新的 UiViewController,它会根据按下的桥信息按钮改变 UiViewController 上显示的信息。
所以基本上我需要知道如何:
1:按下注释中的信息按钮时打开一个 UiViewController。
2:改变 UiViewController 上的信息,以了解按下了哪些桥信息按钮。
这是我目前的情况:
mapView.delegate = self
//bridges
var Bridge1 = CLLocationCoordinate2DMake(48.60,2.90)
var bridge2 = CLLocationCoordinate2DMake(48.61, 2.91)
var bridge3 = CLLocationCoordinate2DMake(48.62, 2.92)
var bridge4 = CLLocationCoordinate2DMake(48.63, 2.93)
var bridge5 = CLLocationCoordinate2DMake(48.64, 2.94)
var bridge6 = CLLocationCoordinate2DMake(48.65, 2.95)
var span = MKCoordinateSpanMake(0.4, 0.4)
var region = MKCoordinateRegion(center: Bridge1, span: span)
mapView.setRegion(region, animated: true)
var Bridge1pin = MKPointAnnotation()
Bridge1pin.coordinate = Bridge1
Bridge1pin.title = "Bridge1"
Bridge1pin.subtitle = "This is bridge 1"
mapView.addAnnotation(Bridge1pin)
var bridge2pin = MKPointAnnotation()
bridge2pin.coordinate = bridge2
bridge2pin.title = "Bridge2"
bridge2pin.subtitle = "This is bridge 2"
mapView.addAnnotation(bridge2pin)
var bridge3pin = MKPointAnnotation()
bridge3pin.coordinate = bridge3
bridge3pin.title = "Bridge3"
bridge3pin.subtitle = "This is bridge 3"
mapView.addAnnotation(bridge3pin)
var bridge4pin = MKPointAnnotation()
bridge4pin.coordinate = bridge4
bridge4pin.title = "Bridge4"
bridge4pin.subtitle = "This is bridge 4"
mapView.addAnnotation(bridge4pin)
var bridge5pin = MKPointAnnotation()
bridge5pin.coordinate = bridge5
bridge5pin.title = "bridge5"
bridge5pin.subtitle = "hello this is bridge 5"
mapView.addAnnotation(bridge5pin)
var bridge6pin = MKPointAnnotation()
bridge6pin.coordinate = bridge6
bridge6pin.title = "bridge6"
bridge6pin.subtitle = "hello this is bridge 6"
mapView.addAnnotation(bridge6pin)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let reuseIdentifier = "pin"
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
if pin == nil {
pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
pin!.pinColor = .Red
pin!.canShowCallout = true
pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
} else {
pin!.annotation = annotation
}
return pin
}
}
您可以了解不同视图之间的导航here in this link.
我会给出一些关于导航的提示和准备导航的数据。
1.检测用户何时点击桥
为此,您只需实现一种名为 calloutAccessoryControlTapped 的标注方法,如下所示。
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
performSegueWithIdentifier("info", sender: view)
// Above line of code is used for the segue operation between multiple MVCs.
}
2。您需要添加 segue 操作
这可以通过控制从主视图拖动到故事板上的详细视图和 select 在弹出窗口和 [=34] 中显示 来完成=]属性检查器将标识符添加为信息
3。准备螺纹桥的模型
为此只需重写 prepareForSegue 方法,如下所示。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//Just validating by checking the same identifier
if (segue.identifier == "info") {
if let annotation = sender as? MKAnnotationView {
let detailViewController = segue.destinationViewController as! DetailViewController
detailViewController.titleText = annotation.annotation?.title ?? ""
detailViewController.detaileText = annotation.annotation?.subtitle ?? ""
}
}
}
4。这是 DetailViewController。
它现在只有两个标签来显示 title 和 subtitle.
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var detailLabel: UILabel!
var titleText: String? { didSet { updateUI() } }
var detaileText: String? { didSet { updateUI() } }
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
private func updateUI() {
self.titleLabel?.text = self.titleText
self.detailLabel?.text = self.detaileText
}
}
例如,我在地图上放置了几个图钉,对应于不同的桥梁位置。单击这些图钉时,每个图钉都有自己的注释,显示它们的标题和副标题。我在这些注释中添加了一个信息按钮。但是我不知道如何打开一个新的 UiViewController,它会根据按下的桥信息按钮改变 UiViewController 上显示的信息。
所以基本上我需要知道如何: 1:按下注释中的信息按钮时打开一个 UiViewController。 2:改变 UiViewController 上的信息,以了解按下了哪些桥信息按钮。
这是我目前的情况:
mapView.delegate = self
//bridges
var Bridge1 = CLLocationCoordinate2DMake(48.60,2.90)
var bridge2 = CLLocationCoordinate2DMake(48.61, 2.91)
var bridge3 = CLLocationCoordinate2DMake(48.62, 2.92)
var bridge4 = CLLocationCoordinate2DMake(48.63, 2.93)
var bridge5 = CLLocationCoordinate2DMake(48.64, 2.94)
var bridge6 = CLLocationCoordinate2DMake(48.65, 2.95)
var span = MKCoordinateSpanMake(0.4, 0.4)
var region = MKCoordinateRegion(center: Bridge1, span: span)
mapView.setRegion(region, animated: true)
var Bridge1pin = MKPointAnnotation()
Bridge1pin.coordinate = Bridge1
Bridge1pin.title = "Bridge1"
Bridge1pin.subtitle = "This is bridge 1"
mapView.addAnnotation(Bridge1pin)
var bridge2pin = MKPointAnnotation()
bridge2pin.coordinate = bridge2
bridge2pin.title = "Bridge2"
bridge2pin.subtitle = "This is bridge 2"
mapView.addAnnotation(bridge2pin)
var bridge3pin = MKPointAnnotation()
bridge3pin.coordinate = bridge3
bridge3pin.title = "Bridge3"
bridge3pin.subtitle = "This is bridge 3"
mapView.addAnnotation(bridge3pin)
var bridge4pin = MKPointAnnotation()
bridge4pin.coordinate = bridge4
bridge4pin.title = "Bridge4"
bridge4pin.subtitle = "This is bridge 4"
mapView.addAnnotation(bridge4pin)
var bridge5pin = MKPointAnnotation()
bridge5pin.coordinate = bridge5
bridge5pin.title = "bridge5"
bridge5pin.subtitle = "hello this is bridge 5"
mapView.addAnnotation(bridge5pin)
var bridge6pin = MKPointAnnotation()
bridge6pin.coordinate = bridge6
bridge6pin.title = "bridge6"
bridge6pin.subtitle = "hello this is bridge 6"
mapView.addAnnotation(bridge6pin)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let reuseIdentifier = "pin"
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
if pin == nil {
pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
pin!.pinColor = .Red
pin!.canShowCallout = true
pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
} else {
pin!.annotation = annotation
}
return pin
}
}
您可以了解不同视图之间的导航here in this link.
我会给出一些关于导航的提示和准备导航的数据。
1.检测用户何时点击桥
为此,您只需实现一种名为 calloutAccessoryControlTapped 的标注方法,如下所示。
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
performSegueWithIdentifier("info", sender: view)
// Above line of code is used for the segue operation between multiple MVCs.
}
2。您需要添加 segue 操作
这可以通过控制从主视图拖动到故事板上的详细视图和 select 在弹出窗口和 [=34] 中显示 来完成=]属性检查器将标识符添加为信息
3。准备螺纹桥的模型
为此只需重写 prepareForSegue 方法,如下所示。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//Just validating by checking the same identifier
if (segue.identifier == "info") {
if let annotation = sender as? MKAnnotationView {
let detailViewController = segue.destinationViewController as! DetailViewController
detailViewController.titleText = annotation.annotation?.title ?? ""
detailViewController.detaileText = annotation.annotation?.subtitle ?? ""
}
}
}
4。这是 DetailViewController。
它现在只有两个标签来显示 title 和 subtitle.
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var detailLabel: UILabel!
var titleText: String? { didSet { updateUI() } }
var detaileText: String? { didSet { updateUI() } }
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
private func updateUI() {
self.titleLabel?.text = self.titleText
self.detailLabel?.text = self.detaileText
}
}