Добавление наблюдателя в поле UIText.никогда не стреляет? - PullRequest
0 голосов
/ 31 мая 2018

Swift 4.0 iOS 11.x

Добавил несколько UITextfields, стал первым респондентом и затем этими наблюдателями.Но они никогда не стреляют?Что мне здесь не хватает?

@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)")
    }
}

Когда я вводю текст в nameTextField или hintTextField, на консоль ничего не выводится.Попытка сделать класс UITextFieldDelegate и действительно установить делегат текстового поля, но все еще не приходит никаких уведомлений?

Ответы [ 2 ]

0 голосов
/ 01 июня 2018

Хорошо, все заработало, с помощью другого уведомления, похоже, работало;но, конечно, не делает этого в данный момент.

private func listenToTextFields() {
    let center = NotificationCenter.default
    let queue = OperationQueue.main
    let alert2Monitor = NSNotification.Name.UITextFieldTextDidEndEditing
    namedObserver = center.addObserver(forName: alert2Monitor, object: nameTextField, queue: queue) { (notification) in
      self.setWayPoint.didSetVariable(image: nil, name: self.nameTextField.text, hint: self.hintTextField.text)
    }
    hintObserver = center.addObserver(forName: alert2Monitor, object: hintTextField, queue: queue) { (notification) in
      self.setWayPoint.didSetVariable(image: nil, name: self.nameTextField.text, hint: self.hintTextField.text)
    }
}
0 голосов
/ 31 мая 2018

Попробуйте использовать пользовательский интерфейс XCode вместо того, чтобы делать это с помощью кода.См. Изображение ниже.

enter image description here

...