geocodeAddressString()
在一起
UITextField
以及我认为它们如何影响我的MKMapView
代码(基于我在这里看到的评论)UITextField
正在使用稍微修改过的扩展程序(最初由' nhgrif'),我在这里找到的目标是能够设置菊花链文本字段,以便按下弹出键盘上的 Next (或 Return )按钮将自动进入所需的下一个(或在某些情况下,以前的文本字段。
private var kAssociationKeyNextField: UInt8 = 0
private var kAssociationKeyPreviousField: UInt8 = 1 // I added this
extension UITextField {
@IBOutlet var nextField: UITextField? {
get { return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField }
set(newField) { objc_setAssociatedObject(self, &kAssociationKeyNextField, newField, .OBJC_ASSOCIATION_RETAIN) }
}
// I added the following
@IBOutlet var previousField: UITextField? {
get { return objc_getAssociatedObject(self, &kAssociationKeyPreviousField) as? UITextField }
set(newField) { objc_setAssociatedObject(self, &kAssociationKeyPreviousField, newField, .OBJC_ASSOCIATION_RETAIN) }
}
}
nextField
设置为 地址 :
previousField
设置为 名称 和{{ 1}}到 城市 :
nextField
设置为 地址 和{{ 1}}到 州 :
previousField
设置为 城市 和{{1转到 邮政编码 :
nextField
设置为 州 - 故意离开previousField
未设置:
nextField
previousField
nextField
是的,我确认我在代码中没有设置断点
class NewLocationViewController: UIViewController, CLLocationManagerDelegate, UITextFieldDelegate {
@IBOutlet weak var doGeoLocate: UISwitch!
@IBOutlet weak var name: UITextField!
@IBOutlet weak var address: UITextField!
@IBOutlet weak var city: UITextField!
@IBOutlet weak var state: UITextField!
@IBOutlet weak var zipcode: UITextField!
@IBOutlet weak var done: UIBarButtonItem!
@IBOutlet weak var map: MKMapView!
var coords: CLLocationCoordinate2D?
var locationManager: CLLocationManager = CLLocationManager()
var currentLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
name.delegate = self
address.delegate = self
city.delegate = self
state.delegate = self
zipcode.delegate = self
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
currentLocation = nil
doGeoLocate.isOn = false
map.isHidden = true
done.isEnabled = false
navigationController?.isNavigationBarHidden = false
navigationController?.isToolbarHidden = false
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if doGeoLocate.isOn == true {
textField.resignFirstResponder()
}
else if textField.nextField == nil {
if (!checkFields()) {
// walk back up chain to find last non-filled text-field...
var tmpField = textField
while ((tmpField.previousField != nil) && (tmpField.previousField?.hasText)!) {
tmpField = tmpField.previousField!
}
tmpField.previousField?.becomeFirstResponder()
}
else {
textField.resignFirstResponder()
}
}
else {
textField.nextField?.becomeFirstResponder()
}
return checkFields()
}
func checkFields() -> Bool {
//... if doGeoLocate switch is on - return true
//... if ALL fields are populated, call geocodeAddress() and return true
//... otherwise return false
}
func geocodeAddress() {
print("GA") //#=#
let geoCoder = CLGeocoder()
let addr = "\(address.text) \(city.text) \(state.text) \(zipcode.text)"
print("ADDR: `\(addr)'")//#=#
geoCoder.geocodeAddressString(addr, completionHandler: {
(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
print("IN geocodeAddressString")//#=#
//if error.localizedDescription.isEmpty == false {
// print("Geocode failed with error: \(error.localizedDescription)")
//}
//else if placemarks!.count > 0 {
let placemark = placemarks![0]
let location = placemark.location
self.coords = location!.coordinate
self.map.isHidden = false
//}
} as! CLGeocodeCompletionHandler) //<<<=== NOTE THIS LINE
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//...
}
@IBAction func toggleGeoLocate(_ sender: AnyObject) {
//...
}
@IBAction func useNewLocation(_ sender: AnyObject) {
//...
}
}
代码是正确的(我在Swift-2的另一个应用中使用过它),但我&#39;当我尝试将其与其他字段链接时,我非常怀疑State和Zipcode Outlets 的重命名方式。
答案 0 :(得分:1)
我建议摆脱那些不同的演员:
func geocodeAddress() {
//...
geoCoder.geocodeAddressString(addr) { placemarks, error in
//...
}
}
最简单的方法是让它为您推断出正确的类型。
关于网点的命名,IB正试图让您的生活更轻松,在没有任何指定名称的情况下为您命名。但是通过这些额外的出口,其默认算法不足。你可以通过在&#34; Document&#34;中自己命名出口来解决这个问题。 &#34; Identity Inspector&#34;。
的一部分