开发者

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?

Tha开发者_JS百科nk 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? `


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜