Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] UITextInputDelegate 개념 (UITextView, UITextField) 본문

iOS 응용 (swift)

[iOS - swift] UITextInputDelegate 개념 (UITextView, UITextField)

jake-kim 2023. 11. 15. 01:26

UITextInputDelegate 개념

  • 텍스트 입력에 대한 델리게이트
    • UITextView와 UITextField가 가지고 있는 delegate

 

  • iOS 3.2+에서 제공

https://developer.apple.com/documentation/uikit/uitextinputdelegate

  • 코드로는 inputDelegate로 접근
    • selectionWillChange: textFIeld나 textView 글자를 눌러서 선택했을때 바로 직전 호출
    • selectionDidChange: textFIeld나 textView 글자를 눌러서 선택하거나 드래그했을때 selected range값이 업데이트된 후  호출
    • 주의) text 자체가 change되는 이벤트들은 문서와는 다르게 실제로 코딩해보면 호출 x
      • textWillChnage: text가 변경되기 직전 호출
      • textDidChnage: text가 변경되고난 후 호출
textField.inputDelegate = self

extension ViewController: UITextInputDelegate {
    func selectionWillChange(_ textInput: UITextInput?) {
        print("selectionWillChange")
    }
    
    func selectionDidChange(_ textInput: UITextInput?) {
        print("selectionDidChange")
    }
    
    func textWillChange(_ textInput: UITextInput?) {
        print("textWillChange")
    }
    
    func textDidChange(_ textInput: UITextInput?) {
        print("textDidChange")
    }
}

테스트)

* 전체 코드: https://github.com/JK0369/ExTextInputDelegate

 * 참고

https://developer.apple.com/documentation/uikit/uitextinputdelegate

Comments