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

老子叫甜甜 提交于 2019-12-08 10:42:40

问题


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.


回答1:


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? `




回答2:


You will have to implement the uitextfielddelegate protocol. Check the documentation for more details.




回答3:


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




回答4:


Connect an action to the text field and choose the "value changed" event, then every time that user changes the value (text) of the text field the action will be executed



来源:https://stackoverflow.com/questions/6997668/how-do-i-know-if-the-text-of-a-uitextfield-has-been-modified-by-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!