【问题标题】:swift passing data in textfield to mapview将文本字段中的数据快速传递到 mapview
【发布时间】:2018-01-16 04:26:27
【问题描述】:

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

还是我做错了?
代码:

@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 日)还有更好的答案吗?虽然我已经重现了这个问题。
欢迎提供更多答案供您参考。

【问题讨论】:

  • VC B中的tableview并不是TableViewController的完整版。只是表格视图。
  • 我的应用图片:imgur.com/JufHrUj
  • 你为什么要放松???你只有两个VC才用这个:navigationController?.popViewController(animated: true)
  • 嗯...我想这样做,因为它使用户能够返回到地图视图页面。从记录页面退出更容易。
  • 呃...展开问题我找到了另一种方法...还有另一个问题:如何在地图视图中获取用户位置? @IBAction func myLocationButtonClicked(_ sender: Any) { myMapView.showsUserLocation = true }

标签: swift uitextfield mkmapview pass-data


【解决方案1】:

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

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)
}

【讨论】:

  • @hkuser0209 使用上述代码获取当前位置
  • 你的 mapViewDelegate 是什么???将此 myLocationManager.delegate = mapViewDelegate 更改为? CLLocationManagerDelegate 到 myLocationManager.delegate = self
  • umm... 它仍然需要“as?CLLocationManagerDelegate”和第一个代码:let userLocation:CLLocation = locations[0] as CLLocation 它显示“使用未解析的标识符“locations””我应该怎么做做什么?
  • @hkuser0209 你用的是同一个委托方法吗??
  • locationManager.delegate = self as? CLLocationManagerDelegate
【解决方案2】:

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

喜欢:

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

从任何地方更新其内容

Manager.tfText = textField.text!

检索数据同上

let variable : String = Manager.tfText

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

【讨论】:

  • 如果我使用 Popover 那该怎么办?
  • 这是一个结构,您在下一秒刚刚更新了它的值,您可以在弹出窗口中访问它的值,您将使用 table 吗?地图 ?标签 ?任何你可以传递价值的地方
  • 那么如果我不需要更新内容,我只使用第一和第三个代码?
  • 那当然是空的,那里什么也不会显示
猜你喜欢
  • 1970-01-01
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
相关资源
最近更新 更多