0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Swift] textFieldの値が変更されたタイミングを取得

Last updated at Posted at 2020-11-05

textFieldの値を更新するタイミングを取得したい、、、
textViewには値が更新するたびに呼ばれるメッソドtextViewDidChange(_:)がありますが、textFieldにはないです。
自分で実装する必要があります

実装

viewDidLoad内に以下を記述。
これで、値が変更するたびに、textFieldDidChangeが呼ばれるようになります。


textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)

あとはtextFieldDidChangeを記述して終わりです。
#selectorを使う際はメソッドの先頭に@objcをつけましょう


    @objc func textFieldDidChange(sender: UITextField) {
        //任意の処理
    }   

参考

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?