개발
Swift TextField 엔터키 이벤트 수신하는 방법!
소소ing
2021. 2. 16. 18:34
반응형
iOS App을 개발하다 보면 TextField의 글자 입력 후 엔터키 이벤트를 수신해야 할때가 있다.
이럴때는 UITextFieldDelegate를 사용하면 된다.
코드 파일 이름이 testViewController.swift 라 가정할때
class testViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var fieldId: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// StoryBoard에서 fieldId 객체의 딜리게이트 연결을 하지 않았다면
self.fieldId.delegate = self
}
// UITextFieldDelegate Return key 이벤트 함수
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == self.fieldId {
// 원하는 액션 지정하기
}
return true
}
}
반응형