UITextField - capture return button event

前端 未结 7 1164
春和景丽
春和景丽 2020-12-04 16:17

How can I detect when a user pressed \"return\" keyboard button while editing UITextField? I need to do this in order to dismiss keyboard when user pressed the \"return\" bu

7条回答
  •  庸人自扰
    2020-12-04 16:55

    Delegation is not required, here's a one-liner:

    - (void)viewDidLoad {
        [textField addTarget:textField
                      action:@selector(resignFirstResponder)
            forControlEvents:UIControlEventEditingDidEndOnExit];
    }
    

    Sadly you can't directly do this in your Storyboard (you can't connect actions to the control that emits them in Storyboard), but you could do it via an intermediary action.

提交回复
热议问题