Swift 4.0 iOS 11.x
Added some UITextfields, became the first responder and than these observers. But they never seem to fire? What am I missing here?
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var hintTextField: UITextField!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
listenToTextFields()
}
private func listenToTextFields() {
let center = NotificationCenter.default
let queue = OperationQueue.main
namedObserver = center.addObserver(forName: NSNotification.Name.UITextViewTextDidChange, object: nameTextField, queue: queue) { (notification) in
print("You edited \(self.nameTextField.text)")
}
hintObserver = center.addObserver(forName: NSNotification.Name.UITextViewTextDidChange, object: hintTextField, queue: queue) { (notification) in
print("You edited \(self.hintTextField.text)")
}
}
When I enter text into the nameTextField or hintTextField, nothing is printed to the console. Tried making the class a UITextFieldDelegate, and indeed setting the textfield delegate, but still no notifications coming my way?
Try it with the XCode UI instead of do it via code. See the image below.