2

I have a UITextField where the user enters an address and selects one of the matching locations that shows from MKLocalSearchCompleter.

If the user selects an address from the autofill option as shown below, the textField is empty. If the user then types anything after the autofill address, it works fine but right after selecting the autofill address, the textField itself is deemed empty.

Code below

    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 {
                        $0.edges.equalToSuperview()
                        $0.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
}

Autofill address suggestion

textField after selecting the autofill address (it's after this that it is showing activeStr is empty)

FSUWX2011
  • 355
  • 1
  • 3
  • 8
  • Those are some big screenshots! Have you set your delegate correctly? – ninehundreds Nov 20 '20 at 22:08
  • @ninehundreds which delegate? I have delegate for MKLocalSearchCompleterDelegate and delegate for item selected when user taps on a location from the search but that is not related to the string being empty. – FSUWX2011 Nov 23 '20 at 15:03
  • You also have to have delegate set for UITextField, otherwise that function will not fire. – ninehundreds Nov 23 '20 at 15:46
  • @ninehundreds Yes it's set. Typing normally in the UITextField works as expected, it's only using the autofill where I get an empty string in the shouldChangeCharactersIn method. If I type after the autofill, it's all good but I can't expect the user to do that. – FSUWX2011 Nov 24 '20 at 15:12
  • I have the very same issue, autofill should not cause this error and should behave similar to the paste function. – Pedro Paulo Amorim May 20 '22 at 15:52

1 Answers1

1

Instead of using:

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

}

switch to:

func textFieldDidChange(_ textField: UITextField) {

}
ninehundreds
  • 1,097
  • 2
  • 21
  • 43
FSUWX2011
  • 355
  • 1
  • 3
  • 8