how to add an action on UITextField return key?

前端 未结 4 1418
孤独总比滥情好
孤独总比滥情好 2020-12-13 01:38

I have a button and text textfield in my view. when i click on the textfield a keyboard appears and i can write on the textfield and i also able to dismiss the keyboard by c

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 01:57

    Ensure "self" subscribes to UITextFieldDelegate and initialise inputText with:

    self.inputText.delegate = self;
    

    Add the following method to "self":

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        if (textField == self.inputText) {
            [textField resignFirstResponder];
            return NO;
        }
        return YES;
    }
    

    Or in Swift:

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField == inputText {
            textField.resignFirstResponder()
            return false
        }
        return true
    }
    

提交回复
热议问题