iOS UITextField-取消自定义 inputView

发布于 2024-10-18 11:40:27 字数 404 浏览 8 评论 0原文

我正在为某些文本字段使用自定义 inputView,但是当我在文本字段上调用 ​​resignFirstResponder 时,自定义输入视图不会忽略...有什么建议吗?

UITextField *f=[[UITextfield alloc] init];
 UIView *view=[[UIView alloc] initWithFrame..];
   [f setInputView:view]; //random example of how to set a textfields input view to a custom view

经过一些研究,这个问题仅在使用模态视图中呈现的 viewController 时出现......否则它工作正常......

谢谢

-Daniel

I am using a custom inputView for some textfield, however when i call resignFirstResponder on the textfield, the custom input view does not dismiss...any suggestions?

UITextField *f=[[UITextfield alloc] init];
 UIView *view=[[UIView alloc] initWithFrame..];
   [f setInputView:view]; //random example of how to set a textfields input view to a custom view

After some research, this issue only occurs when working with a viewController thats presented in a modal view...it works ok otherwise...

Thanks

-Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情何以堪。 2024-10-25 11:40:28

您应该在视图本身或其任何超级视图上调用 endEditing:,而不是调用 resignFirstResponderendEditing: 将在所有子视图中搜索任何具有第一响应者状态的视图,并要求其退出。

endEditing: 采用布尔标志作为参数。如果设置,将强制第一响应者辞职;否则,您可以允许它保持焦点(例如,如果输入无效)。对于 UITextField,这是由委托的 shouldEndEditing: 方法确定的。

一般来说,一个好的使用模式是:

- (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)
    }
}

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)
    }
}
梦里南柯 2024-10-25 11:40:28

如果您使用模态视图,请确保启用自动键盘关闭。将此代码添加到您的视图控制器中:

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文