swift 将文本字段中的数据传递到地图视图

swift passing data in textfield to mapview

我正在制作一个应用程序来搜索位置。它包含一个VC(viewcontroller)来显示搜索记录。
Q1。如何在文本字段中获取输入并在地图视图中显示位置?
Q2。我无法解除 viewcontroller。我已经完成了情节提要中的代码和连线。但是还是什么都不做
Q3.如何在单击按钮时将 VC A 中的文本字段中的数据传递到 VC B 中的表格视图?

还是我做错了?
代码:

@IBAction func unwind(segue: UIStoryboardSegue) {
    viewController?.dismiss(animated: true)

    self.performSegue(withIdentifier: "unwindToMap", sender: self)

}  

override func viewDidLoad() {
    super.viewDidLoad()

    //Check for Location Services
    if (CLLocationManager.locationServicesEnabled()) {
        myLocationManager = CLLocationManager()
        myLocationManager.delegate = mapViewDelegate as? CLLocationManagerDelegate
        myLocationManager.desiredAccuracy = kCLLocationAccuracyBest
        myLocationManager.requestAlwaysAuthorization()
        myLocationManager.requestWhenInUseAuthorization()
    }
    mapViewDelegate = searchBox.text as? MKMapViewDelegate
}

@IBAction func searchButtonClick(_ sender: Any) {


    let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    let span = MKCoordinateSpanMake(0.005, 0.005)
    let region = MKCoordinateRegion(center: location, span: span)
    let annotation = MKPointAnnotation()


    annotation.coordinate = location
    annotation.title = searchBox.text!


    /*
    //find the words
    let _ : UITextPosition = searchBox.beginningOfDocument
    let _ : UITextPosition = searchBox.endOfDocument
    let _ : UITextRange = searchBox.selectedTextRange!

    // Get cursor position
    if let selectedRange = searchBox.selectedTextRange {
        _ = searchBox.offset(from: searchBox.beginningOfDocument, to: selectedRange.start)
    }
    searchBox.selectedTextRange = searchBox.textRange(
        from: searchBox.beginningOfDocument,                                         to: searchBox.endOfDocument)
    */

    // myMapView.add(location as! MKOverlay)
    myMapView.addAnnotation(annotation)
    myMapView.setRegion(region, animated: true)


}

如果您的回答有用,我将不胜感激。

编辑:
(2019 年 7 月 24 日)还有更好的答案吗?虽然我已经重现了这个问题。
欢迎回答更多,供大家参考。

要将数据从一个屏幕传递到另一个屏幕,您可以使用结构

喜欢:

struct Manager
{
    static var tfText : String = ""
}

从任何地方更新其内容使用

Manager.tfText = textField.text!

检索数据同上

let variable : String = Manager.tfText

您可以使用导航控制器来弹出控制器,如果需要使用展开,您是否连接了源vc以在故事板中展开转场

对于当前位置,您可以使用以下代码:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation:CLLocation = locations[0] as CLLocation

    let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    mapView.setRegion(region, animated: true)

    // Drop a pin at user's Current Location
    let myAnnotation: MKPointAnnotation = MKPointAnnotation()
    myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
    myAnnotation.title = "Current location"
    mapView.addAnnotation(myAnnotation)
}