Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Continuously check if statement

I would like my app to continuously loop through an if statement. How would I do this?

4 Answers

this should do the trick. create an IBAction for your text field and for the event select "editing changed". that will be called everytime the text in your field changes when editing so you could put your if statement in the implementation of that and compare the last character in your text field to the certain character you need

read more here

Thank you so much!

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Depending on what language you're using you need to have an event listener to execute a function to check the if statement. This is easier on the computer than looping continually.

May I ask what you are trying to achieve? It would be interesting to know.

I would like a label to change if the user has started typing a certain letter into the textfield

look into this method textField:shouldChangeCharactersInRange:replacementString:. Its a delegate method of UITextField. More info can be found here

A brief explanation is provided: The text field calls this method whenever the user types a new character in the text field or deletes an existing character.

maybe have someting like

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

      if (string == @"a") {

           //do label stuff here

      } 

     return YES;  

}

im not familiar with the method and not sure of how it behaves, but something like that might be what your are looking for

edit: this is probably not the way the way to go, see my above post for an easier way to do it.