ios 禁用文本字段的编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29858796/
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
Disable editing of a text field
提问by Greg
I want to start by saying this is my first project and I am trying hard to find answers myself before posting here. I thought I had found the code to accomplish this. I have no errors but when I run, the field is still editable. So, how can I disable editing of my field when the rule I have set is true?
我想首先说这是我的第一个项目,我正在努力寻找答案,然后再在这里发帖。我以为我已经找到了实现这一点的代码。我没有错误,但是当我运行时,该字段仍然是可编辑的。那么,当我设置的规则为真时,如何禁用编辑我的字段?
if sport.count == 1 {
enterSport.text = sport[0] as String //need to convert to a string
enterSport.editing; false //do not allow editing
} else {
//do nothing
}
I have defined the array previously so the if statement is true. Thank you for you assistance.
我之前已经定义了数组,因此 if 语句为真。谢谢你的帮助。
回答by Dima
enterSport.userInteractionEnabled = false
instead of editing
.
enterSport.userInteractionEnabled = false
而不是editing
.
editing
is a read-only property to indicate if the field is currently being edited or not.
editing
是一个只读属性,用于指示当前是否正在编辑该字段。
回答by Duncan C
To summarize the answers and comments above:
总结上面的答案和评论:
editing is a readonly property. You can't set it.
编辑是一个只读属性。你不能设置。
If it's a UITextView, it has an editable property which you can set to false.
如果它是一个 UITextView,它有一个可编辑的属性,您可以将其设置为 false。
If it's a UITextField, you need to use the enabled property or the userInteractionEnabled property. Either of those will prevent editing. As
如果是 UITextField,则需要使用 enabled 属性或 userInteractionEnabled 属性。其中任何一个都会阻止编辑。作为
回答by cylov
With Swift 3 Apple changed the property userInteractionEnabled
to isUserInteractionEnabled
.
在 Swift 3 中,Apple 将该属性更改userInteractionEnabled
为isUserInteractionEnabled
.
The code looks like this:
代码如下所示:
textfield.isUserInteractionEnabled = true
textfield.isUserInteractionEnabled = true