简体   繁体   English

我如何知道UITextField的文本是否已被用户修改

[英]How do I know if the text of a UITextField has been modified by user

I work on a project for iPhone iOS 4 and Xcode 4. 我正在为iPhone iOS 4和Xcode 4开发一个项目。

There is a method to know if the text of a UITextField ha been modified by the user? 有一种方法可以知道用户是否修改了UITextField的文本?

For example,I have 2 UITextField, textFieldA and textFieldB, and suppose that textField contains some text as "abc". 例如,我有2个UITextField,textFieldA和textFieldB,并假设textField包含一些文本为“ abc”。

The user first tap in textFieldA (keyboard opens), and then in textFieldB. 用户首先点击textFieldA(打开键盘),然后点击textFieldB。 How can I know if the text in textFieldA has been modified by the user? 我如何知道textFieldA中的文本是否已被用户修改?

Thank you. 谢谢。

How about: First subscribe to the text field delegate in your header file with: 怎么样:首先使用以下命令订阅头文件中的文本字段委托:
<UITextFieldDelegate>

Then somewhere in your implementation file (.m), you can add: myTextField.delegate = self; 然后,可以在实现文件(.m)中的某个位置添加: myTextField.delegate = self;

Now you can access the delegate methods for text fields. 现在,您可以访问文本字段的委托方法。 This means you can now use: 这意味着您现在可以使用:

    -(void)textFieldDidBeginEditing:(UITextField *)textField

and -(void)textFieldDidEndEditing:(UITextField *)textField -(void)textFieldDidEndEditing:(UITextField *)textField

Why not store the value of your text field in textFieldDidBeginEditing and compare it to it's value in textFieldDidEndEditing? 为什么不将文本字段的值存储在textFieldDidBeginEditing中,并将其与textFieldDidEndEditing中的值进行比较? ` `

Better to have the text field send a UIControlEventValueChanged action message to your controller. 最好让文本字段将UIControlEventValueChanged操作消息发送到您的控制器。 This way you don't need to wonder if the user actually edited the text field. 这样,您无需怀疑用户是否实际编辑了文本字段。 You can either wire the target/action in IB or do it programmatically: 您可以在IB中连接目标/操作,也可以通过编程方式进行操作:

[myTextField addTarget:self 
                action:@selector(textFieldValueChanged:)
      forControlEvents:UIControlEventValueChanged];

Then implement the appropriate action in the target: 然后在目标中执行适当的操作:

- (IBAction)textFieldValueChanged:(UITextField *)sender {
    // text field value was changed
}

This is discussed in more detail for this question 这更详细的讨论这个问题

将操作连接到文本字段并选择“值已更改”事件,然后每次用户更改文本字段的值(文本)时,将执行该操作

You will have to implement the uitextfielddelegate protocol. 您将必须实现uitextfielddelegate协议。 Check the documentation for more details. 查看文档以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM