【发布时间】: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