views:

22

answers:

2

Hi, i'm trying to clear a UITextField whenever the UIControlEventEditingChanged event is being performed.

However, when I set the text to nothing, the UIControlEventEditingChanged event is being called again, and this is the way it keeps going.

This is my code:

- (void)updateText {
    //other code
    textfield.text = @"";
    //other code
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [textfield addTarget:self action:@selector(updateText) forControlEvents:UIControlEventEditingChanged];
}

I would like to be able to set the text of the text field without it looping infinitely!

Any help appreciated.

A: 

Not sure this is the best way, but before you do your programmatic update to the textfield you could remove your UIControlEventEditingChanged listener and then add it again afterwards.

willcodejavaforfood
+1  A: 

do

- (void)updateText {
    //other code
    if (![textfield.text isEqualToString:@""]){
       textfield.text = @"";
    }
    //other code
}

it is probably not the most elegant solution but it is very quick and will surely work

Guy Ephraim