ios iPhone/iPad:如何使 UITextField 只读(但未禁用)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7949071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone/iPad: How to make UITextField readonly (but not disabled)?
提问by newman
I understand this question has been asked before, but I'm not satisfied with the answers, i.e. by making it disabled. There is a fundamental difference: Disabled view doesn't fire events, but for a read-only view, it should still fire event like (e.g. TouchUpInside), and I need it. Only thing I don't want is the keyboard input.
我知道以前有人问过这个问题,但我对答案不满意,即禁用它。有一个根本区别:禁用视图不会触发事件,但对于只读视图,它仍然应该触发事件(例如 TouchUpInside),而我需要它。我唯一不想要的是键盘输入。
The reason is that I have several input fields, some can useUITextField
directly, others are not. I want to have them look similar. So, I'd like to use UITextField
to display all of them. Some of them need to be read-only so that I can use touch up event for alternative input.
原因是我有几个输入框,有的可以UITextField
直接使用,有的不行。我想让它们看起来相似。所以,我想用UITextField
来显示所有这些。其中一些需要是只读的,以便我可以使用 touch up 事件作为替代输入。
Or there might be a completely different way to do it?
或者可能有一种完全不同的方式来做到这一点?
EDIT: This is for my MonoTouch project. I have very limited knowledge of Objective-c.
编辑:这是我的 MonoTouch 项目。我对Objective-c的了解非常有限。
回答by chown
Say you have 2 text field instance variables connected to text fields you created in the Interface Builder. Lets call them myReadOnlyTextField
and myEditableTextField
. Make sure you connect the delegate
property of each text field in the Interface Builder to the view controller ("File's Owner")[1]. Now, in the view controller @implementation
(.m file), use the method textFieldShouldBeginEditing:
and put in some logic to determine which text field you want to allow editing and which to not allow editing; something like this:
假设您有 2 个文本字段实例变量连接到您在 Interface Builder 中创建的文本字段。让我们打电话给他们myReadOnlyTextField
和myEditableTextField
。确保将delegate
Interface Builder 中每个文本字段的属性连接到视图控制器(“文件所有者”)[1]。现在,在视图控制器@implementation
(.m 文件)中,使用该方法textFieldShouldBeginEditing:
并放入一些逻辑来确定要允许编辑哪些文本字段以及不允许编辑哪些文本字段;像这样:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
BOOL editable;
if (textField == myReadOnlyTextField) {
editable = NO;
} else if (textField == myEditableTextField) {
editable = YES;
} else {
// editable = YES/NO/Other Logic
}
return editable;
}
From the UITextFieldDelegate
Documentation:
textFieldShouldBeginEditing:
Asks the delegate if editing should begin in the specified text field.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
Parameters
textField
- The text field for which editing is about to begin.Return Value
YES
if an editing session should be initiated; otherwise,NO
to disallow editing.Discussion
When the user performs an action that would normally initiate an editing session, the text field calls this method first to see if editing should actually proceed. In most circumstances, you would simply return YES from this method to allow editing to proceed.Implementation of this method by the delegate is optional. If it is not present, editing proceeds as if this method had returned YES.
textFieldShouldBeginEditing:
询问代理是否应在指定的文本字段中开始编辑。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
参数
textField
- 即将开始编辑的文本字段。
YES
如果应该启动编辑会话,则返回值;否则,NO
禁止编辑。讨论
当用户执行通常会启动编辑会话的操作时,文本字段首先调用此方法以查看编辑是否应实际进行。在大多数情况下,您只需从此方法返回 YES 即可继续进行编辑。委托执行此方法是可选的。如果它不存在,则编辑继续进行,就好像此方法已返回 YES 一样。
UITextField
Documentationis a good read also.
UITextField
文档也很好读。
[1]You can do this programmatically as well. Here is an example:
[1]您也可以以编程方式执行此操作。下面是一个例子:
- (void)viewDidLoad {
[super viewDidLoad];
// .....
myReadOnlyTextField.delegate = self;
myEditableTextField.delegate = self;
}
回答by m8labs
Despite the fact you need no keyboard, textField:shouldChangeCharactersInRange:replacementString:
is very useful. It prevents text field from editing but still leaves it selectable in contrast to textFieldShouldBeginEditing:
.
尽管您不需要键盘,但textField:shouldChangeCharactersInRange:replacementString:
它非常有用。它可以防止文本字段被编辑,但与textFieldShouldBeginEditing:
.
In monotouch:
在单点触控中:
var txt = new UITextField();
txt.ShouldChangeCharacters += (field, range, replacementString) => false;
回答by Peter Bournakas
You can have two options: a) to use ShouldBeginEditing with return false, but you can't use PickerView as InputView in your text field. b) to use ShouldChangeCharacters that will prevent the editing but will allow to use InputView with PickerView.
您可以有两个选择:a) 使用 ShouldBeginEditing 并返回 false,但您不能在文本字段中使用 PickerView 作为 InputView。b) 使用将阻止编辑但允许将 InputView 与 PickerView 一起使用的 ShouldChangeCharacters。
回答by Menno Luimstra
because you are working in MonoTouch you could use the following line of code:
因为您在 MonoTouch 中工作,所以可以使用以下代码行:
myReadOnlyButton.ShouldBeginEditing = t =>
{
//event code
return false;
};
回答by Vinoth kanna
Objective C:
目标 C:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
Swift :
斯威夫特:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
回答by Ben G
I personally use borderStyle = .none + userInteractionEnabled = false to make it look like a regular label.
我个人使用 borderStyle = .none + userInteractionEnabled = false 使它看起来像一个普通标签。