简体   繁体   中英

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.

There is a method to know if the text of a UITextField ha been modified by the user?

For example,I have 2 UITextField, textFieldA and textFieldB, and suppose that textField contains some text as "abc".

The user first tap in textFieldA (keyboard opens), and then in textFieldB. How can I know if the text in textFieldA has been modified by the user?

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;

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

Why not store the value of your text field in textFieldDidBeginEditing and compare it to it's value in textFieldDidEndEditing? `

Better to have the text field send a UIControlEventValueChanged action message to your controller. 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:

[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. Check the documentation for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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