Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active July 4, 2016 22:36
Show Gist options
  • Save KentarouKanno/252ed29320457723d163 to your computer and use it in GitHub Desktop.
Save KentarouKanno/252ed29320457723d163 to your computer and use it in GitHub Desktop.
UITextFieldDelegate

UITextFieldDelegate

★ テキストフィールドを編集する直前に呼び出される

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {

    // falseを返すとキーボードが表示されない
    return true
}

★ テキストフィールドを編集する直後に呼び出される

func textFieldDidBeginEditing(textField: UITextField) {
    
    // textFieldShouldBeginEditingの後、キーボード表示の前に呼ばれる
}

★ キーボードから文字を入力した際呼び出される

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    //=> range = location=0, length=0 文字が入力された位置
    //=> string = "a" 入力された文字列
   
    // falseを返すと入力された文字が追加されない
    return true
}

★ UITextFieldのMaxLenghtを設定する

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    // 最大文字数を設定
    let maxLength: Int = 7
    
    // 入力済みの文字と入力された文字を合わせる
    var str = textField.text + string
 
    // 文字数を判定する
    if countElements("\(str)") < maxLength {
        return true
    }
    return false
}

★ クリアボタンがタップされた時に呼ばれる

func textFieldShouldClear(textField: UITextField) -> Bool {

    // falseにすると入力されている文字がクリアされない
    return true
}

★ キーボードのリターンキー押下時に呼ばれる

func textFieldShouldReturn(textField: UITextField) -> Bool {
        
    // キーボードを閉じる処理等
    textField.resignFirstResponder()
    return true
}

★ テキストフィールドの編集が終了する直前に呼び出される

func textFieldShouldEndEditing(textField: UITextField) -> Bool {

    // falseを返すとキーボードが非表示にならない
    return true
}

★ テキストフィールドの編集が終了する直後に呼び出される

func textFieldDidEndEditing(textField: UITextField) {

    // textFieldDidEndEditingの後、キーボード非表示になる前に呼ばれる        
}

★ まとめ

// MARK: - UITextFieldDelegate.

extension ViewController: UITextFieldDelegate {

    // フォーカスが当たる際に呼び出されるメソッド(編集の可否を定義可能).
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        print(#function)
        return true
    }

    // フォーカスがあたった際に呼び出されるメソッド.
    func textFieldDidBeginEditing(_ textField: UITextField) {
        print(#function)
    }

    // フォーカスが外れる際に呼び出されるメソッド(編集終了の可否を定義可能).
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        print(#function)
        return true
    }

    // フォーカスが外れた際に呼び出されるメソッド.
    func textFieldDidEndEditing(_ textField: UITextField) {
        print(#function)
    }

    // 入力に変更があった際に呼び出されるメソッド.
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        print(#function)
        return true
    }

    // クリアボタンが押された際に呼び出されるメソッド.
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        print(#function)
        return true
    }

    // Returnキータップ時に呼び出されるメソッド(Returnキータップの可否を定義可能).
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print(#function)
        textField.resignFirstResponder()
        return true
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment