iOS UITextField- dismissing custom inputView

时光怂恿深爱的人放手 提交于 2019-12-04 04:53:43

Instead of calling resignFirstResponder, you should call endEditing: on the view itself or any of its superviews. endEditing: will search all subviews for any view that has first responder status, and ask it to resign.

endEditing: takes a boolean flag as a parameter. If set, it will force the first responder to resign; otherwise, you can allow it to keep focus (if input is invalid, for example). For a UITextField, this is determined by the delegate's shouldEndEditing: method.

In general, a good pattern to use is:

- (void)saveAction:(id)sender
{
    if ([self.view endEditing:NO]) {
        NSString *text = self.textField.text;
        // save text, login, do whatever
    } else {
        // show an alert (or rely on whatever refused to resign to inform the user why)
    }
}

If you use a modal view, make make sure to enable automatic keyboard dismissal. Add this code to your view controller:

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!