【问题标题】:How to disable UITextField's edit property? [duplicate]如何禁用 UITextField 的编辑属性? [复制]
【发布时间】:2024-01-15 16:07:02
【问题描述】:

可能重复:
Easy way to disable a UITextField?

我有一个 UITextField。我想禁用它的编辑属性。我只想在其中显示一个文本。

我怎样才能实现它?。

【问题讨论】:

  • 为了显示文本,你应该使用 UILabel 而不是 UITextField(它用于输入一些字符串)
  • 我为此使用myTextField.enabled = false

标签: ios uikit


【解决方案1】:

要禁用 UITextField 中的编辑,您需要在委托方法 textFieldShouldBeginEditing: 中返回 NO

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return NO;
}

在 UITextView 中禁用编辑将属性editable 设置为NO

[textView setEditable:NO];

【讨论】:

  • 如果您使用 UITextField,请不要忘记在 IB 中设置委托或通过代码 textField.delegate = self;
  • 方法调用textViewShouldBeginEditing:而不是textFieldShouldBeginEditing:
  • @padde, textViewShouldBeginEditing - UITextView 的方法。我们正在谈论 UITextField
  • [textView setEnabled:NO];
  • @DavidDouglas @Blasco73 [textView setEnabled:NO]; 禁用整个 TextView,这是错误的。有 editable 属性禁用编辑 textview 的可能性。