使用 inputView 在 iPhone 上显示 UIPicker 时防止直接键入 UITextField

发布于 2024-12-15 20:47:56 字数 416 浏览 6 评论 0原文

我的代码工作得很好,通过使用 UITextField 的 inputViewUITextField 对象(在 UITableViewCell 中)上显示选择器对象。这会停止显示键盘,但不会阻止某人直接在字段中输入内容。

公平地说,他们只能粘贴一些内容,但即使如此,如果您不想在字段中添加自定义值,也不应该允许这样做。

有没有办法关闭文本输入?我已经尝试过:

return NO;

textFieldShouldBeginEditing() 委托方法中,但这仅用于阻止字段操作 - 因此不会出现选择器。

还有其他方法可以防止这种行为吗?

提前致谢。

I have code working very nicely to show a picker on a UITextField object (in a UITableViewCell) by using the inputView of the UITextField object. This stops the keyboard being shown but does not stop someone directly typing into the field.

To be fair they can only paste something in but even so that should not be allowed if you do not want a custom value in the field.

Is there anyway of turning the text input off? I've tried:

return NO;

in the textFieldShouldBeginEditing() delegate method but this only serves to prevent the field operating at all - so no picker comes up.

Are there any other ways of preventing this behaviour?

Thanks in advance.

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

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

发布评论

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

评论(3

同展鸳鸯锦 2024-12-22 20:47:56

您可以使用 UITextFieldDelegate 方法,

-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string

每当尝试更改文本字段中的文本时都会调用该方法。您可以做一个简单的检查,以确保 string 值是您期望的值(来自选择器视图的值),否则丢弃它。如果string值是你想要的,则返回YES,否则返回NO

You can use the UITextFieldDelegate method

-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string

This will be called any time that something tries to change text in your text field. You can do a simple check that the string value is something you would expect it to be (something from your picker view), otherwise discard it. If the string value is what you want, return YES, otherwise return NO

浮世清欢 2024-12-22 20:47:56

使用屏幕外的另一个 UITextfield 怎么样? uipicker 是屏幕外文本字段的输入视图。在日期选择器更改时的处理程序中,指定要更新原始文本字段的文本属性。点击原始文本字段以调出选择器可以通过以下几种方式之一进行处理:
1. 文本字段始终设置为 userInteractionEnabled = NO,并且其上方有一个不可见的按钮用于检测触摸。
2. 单元格中的文本字段被标签替换,并由单元格通过 didSelectRowAtIndexPath 检测到触摸。
3. 单元格中的文本字段,在检测到触摸后,委托将 didBeginEditing 中的文本字段设置为 userInteractionEnabled = NO(不确定这是否有效)。

想想看,您甚至可以尝试#3,而无需创建第二个文本字段。

How about using another UITextfield that's offscreen? The uipicker is then the inputview for the offscreen textfield. In the handler for when the date picker changes, the original textfield's text property is specified to be updated. Tapping on the original textfield to bring up the picker is handled one of several ways:
1. The textfield is always set to userInteractionEnabled = NO, and there's an invisible button over it that detects touches.
2. The textfield in the cell is replaced by a label, and touching in instead detected by the cell with didSelectRowAtIndexPath.
3. The textfield in the cell, after it detects the touch, the delegate then sets the textfield in didBeginEditing to userInteractionEnabled = NO (not sure if this would work).

Come to think of it you could even try #3 without creating a second textfield.

埋情葬爱 2024-12-22 20:47:56

您要求做的是禁用子菜单。

要执行此子类 UITextField 并实现以下方法,

(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

这将阻止菜单显示。

What you are asking to do is disable sub menus.

To do this sub-class UITextField and implement the following method

(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

This will prevent the menu from showing.

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