iOS 从联系人中选择自动填充地址后,UITextField 文本字段为空

iOS UITextField textfield empty after selecting autofill address from contacts

我有一个 UITextField,用户可以在其中输入地址并选择从 MKLocalSearchCompleter 显示的匹配位置之一。

如果用户从自动填充选项中选择一个地址,如下所示,文本字段为空。如果用户随后在自动填充地址后键入任何内容,它工作正常但在选择自动填充地址后,文本字段本身被视为空。

下面的代码

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    if textField == locationTextField.textfield {
        
        let activeStr = textField.text!
        if (activeStr.length() >= 2) {
            
            if !suggestionsShowing {
                
                locationSelectorView = LocatorSelectorView (using: [], rowHeight: selectorRowHt)
                if let locationSelectorView = locationSelectorView {
                    locationSelectorView.delegate = self
                    addressSuggestionView.addSubview(locationSelectorView)
                    
                    locationSelectorView.snp.makeConstraints {
                        [=11=].edges.equalToSuperview()
                        [=11=].height.equalTo(self.selectorRowHt * CGFloat(5) + 20)
                    }
                    
                    stackview.insertArrangedSubview(addressSuggestionView, at: 1)
                    
                    suggestionsShowing = true
                }
            }
            
            self.geocoder?.appleGeocode(using: "\(textField.text!)\(string)", complete: { (error) in
                
            })
        }
    }
    return true
}

而不是使用:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

}

切换到:

func textFieldDidChange(_ textField: UITextField) {

}