S
S
Stanislav Korolevskiy2018-04-25 20:47:07
Swift
Stanislav Korolevskiy, 2018-04-25 20:47:07

How to track the state of a textField?

How to understand that the user has started typing text in a textField? By type willSet and didSet for a variable

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Beltyukov, 2018-04-26
@korolevsky_s

UITextField inherits from UIControl, which means you can subscribe to its controlEvents. Example:

override func viewDidLoad() {
        super.viewDidLoad()
        textField.addTarget(self, action: #selector(editingBegan(_:)), for: .editingDidBegin)
        textField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
        textField.addTarget(self, action: #selector(editingEnded(_:)), for: .editingDidEnd)
    }

    @objc func editingBegan(_ textField: UITextField) {
        //пользователь попал в поле, но еще ничего не ввел
    }

    @objc func editingChanged(_ textField: UITextField) {
        //текст изменился
    }

    @objc func editingEnded(_ textField: UITextField) {
        //ввод окончен, к примеру нажали "return" на клавиатуре
    }

T
tegrato, 2020-06-01
@tegrato

It is easier to write not just a function, but to make an @IBAction, for which, through the StoryBoard, specify after which event it should work:
- EditingDidBegin,
- EditingDidEnd,
etc... - which one is needed.
5ed44417dc062950037535.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question