0
0

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.

UITextFieldDelegateのメソッド

Last updated at Posted at 2021-09-01

はじめに

今回はUITextFieldDelegateのメソッドについてまとめます.自分用のメモ目的です.

環境

Xcode 12.5.1
Swift 5.4.2

UITextFieldDelegateのメソッド

1. textFieldShouldBeginEditing

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return true
    }
呼ばれるタイミング できること
タップされたとき,入力可能になる直前 trueでテキスト入力可能,falseで入力不可

2. textFieldDidBeginEditing

    func textFieldDidBeginEditing(_ textField: UITextField) {
        print(textField.text)//textFieldに入力された値をOptional型で出力
    }
呼ばれるタイミング できること
タップされて入力可能になったあと テキストの値を取得

3. textFieldShouldEndEditing

    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        return true
    }
呼ばれるタイミング できること
キーボードのReturnキーが押されて入力が完了する直前 trueで編集を停止,falseはそのまま

4. textFieldDidEndEditing

    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
        print(textField.text)//textFieldに入力された値をOptional型で出力
    }
呼ばれるタイミング できること
キーボードが閉じたあと テキストの値を取得

5. textFieldShouldClear

    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        return true
    }
呼ばれるタイミング できること
キーボードのClearキーが押されたとき trueでクリア,falseは何もしない

6. textFieldShouldReturn

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()//キーボードをを閉じる
        return true
    }
呼ばれるタイミング できること
キーボードのReturnキーが押されたとき キーボードを閉じるなど

7. shouldChangeCharactersIn

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return true
    }
呼ばれるタイミング できること
テキスト編集中 入力文字数の制限など

参考

[初心者向け]UITextFieldDelegate

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?