gpt4 book ai didi

ios - iPhone/iPad : How to make UITextField readonly (but not disabled)?

转载 作者:IT王子 更新时间:2023-10-29 08:13:44 28 4
gpt4 key购买 nike

我知道之前有人问过这个问题,但我对答案不满意,即禁用它。有一个根本区别:禁用 View 不会触发事件,但对于只读 View ,它仍应触发事件(例如 TouchUpInside),我需要它。我唯一不想要的是键盘输入。

原因是我有几个输入框,有的可以直接使用UITextField,有的则不能。我想让它们看起来相似。所以,我想使用 UITextField 来显示所有这些。其中一些需要是只读的,以便我可以使用触摸事件进行替代输入。

或者可能有一种完全不同的方式来做到这一点?

编辑:这是我的 MonoTouch 项目。我对 Objective-c 的了解非常有限。

最佳答案

假设您有 2 个文本字段实例变量连接到您在 Interface Builder 中创建的文本字段。让我们称它们为 myReadOnlyTextFieldmyEditableTextField。确保将 Interface Builder 中每个文本字段的 delegate 属性连接到 View Controller (“文件的所有者”)[1]。现在,在 View Controller @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;
}

来自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.

UITextField Documentation也是一本好书。


[1] 您也可以通过编程方式执行此操作。这是一个例子:

- (void)viewDidLoad {
[super viewDidLoad];
// .....
myReadOnlyTextField.delegate = self;
myEditableTextField.delegate = self;
}

关于ios - iPhone/iPad : How to make UITextField readonly (but not disabled)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7949071/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com