Reputation: 551
i'm trying to add a comma separator for every third digit that user enters in textfield right away while typing for iOS app using swift 4.0 and Xcode 9. so far i've added these lines of code to my viewdidload method:
self.mainTextField.delegate = self
self.mainTextField(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
ive also added this function to my view controller:
func textFieldDidChange(textField: UITextField) {
if textField == self.mainTextField {
// Some locales use different punctuations.
var textFormatted = textField.text?.replacingOccurrences(of: ",", with: "")
textFormatted = textFormatted?.replacingOccurrences(of: ".", with: "")
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
if let text = textFormatted, let textAsInt = Int(text) {
textField.text = numberFormatter.string(from: NSNumber(value: textAsInt))
}
}
}
but i keep receiving this error from Xcode : Cannot call value of non-function type 'UITextField' on this line:
self.mainTextField(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
Upvotes: 0
Views: 637
Reputation: 759
You forgot the method name addTarget
. Instead of
self.mainTextField(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
use
self.mainTextField.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
Upvotes: 0