如何禁用UITextField编辑但仍然接受触摸? [英] How to disable UITextField editing but still accept touch?
问题描述
我正在制作一个UITextField
,其UIPickerView
作为inputView
.一切都很好,除了我可以通过复制,粘贴,剪切和选择文本进行编辑,而且我不希望这样做.只有选择器才能修改文本字段.
I'm making a UITextField
that has a UIPickerView
as inputView
. Its all good, except that I can edit by copy, paste, cut and select text, and I don't want it. Only the Picker should modify text field.
我了解到可以通过将setEnabled
或setUserInteractionEnabled
设置为NO
来禁用编辑.好的,但是TextField停止响应触摸并且选择器没有显示.
I've learned that I can disable editing by setting setEnabled
or setUserInteractionEnabled
to NO
. Ok, but the TextField stop responding to touching and the picker don't show up.
我该怎么做?
推荐答案
使用文本字段委托,有一个方法
Using the textfield delegate, there's a method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
从此返回否,并且用户尝试编辑文本的任何尝试都将被拒绝.
Return NO from this, and any attempt by the user to edit the text will be rejected.
这样,您可以使该字段保持启用状态,但仍然可以防止人们在其中粘贴文本.
That way you can leave the field enabled but still prevent people pasting text into it.
这篇关于如何禁用UITextField编辑但仍然接受触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!