[Swift] 스위프트 키보드 엔터 키 누를 때 키보드 내리기 (keyboard hide when return event)

class ViewController: UIViewController, UITextFieldDelegate {   //UITextFieldDelegate 추가 해주기
    @IBOutlet weak var nameTextField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        nameTextField.delegate = self  // delegate를 설정해주어야 키 이벤트와 통신이 됨
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {   // UITextFieldDelegate 추가하므로써 함수를 호출 할 수 있음
        textField.resignFirstResponder()  // 엔터(return)을 누르면 키보드가 내려가게 됨
        return true
    }

}

댓글