How do I dismiss the iOS keyboard?

后端 未结 16 1079
情书的邮戳
情书的邮戳 2020-11-29 20:00

I have a UITextfield that i\'d like to dismiss the keyboard for. I can\'t seem to make the keyboard go away no matter what code i use.

16条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 20:22

    If you don't know which textField is the first responder you can find it. I use this function:

    UIView *resignFirstResponder(UIView *theView)
    {
        if([theView isFirstResponder])
        {
            [theView resignFirstResponder];
            return theView;
        }
        for(UIView *subview in theView.subviews)
        {
            UIView *result = resignFirstResponder(subview);
            if(result) return result;
        }
        return nil;
    }
    

    Then in your code call:

        UIView *resigned = resignFirstResponder([UIScreen mainScreen]);
    

提交回复
热议问题