You can use addTarget:action:forControlEvents:
with UIControlEventValueChanged
(e.g: [textfield addTarget:self action:@selector(usernameChanged:) forControlEvents:UIControlEventValueChanged
). This will inform you AFTER the text changes (so when you receive usernameChanged:
you'll have the last modifications).
It's probably a good idea that instead of sending a request to the server each time the username changes you either send a request when the textfield looses caption or when some time passes from the last change (you can use a NSTimer
for this).
You should use a single asynchronous request. If the request is active and the textfield is edited again you should cancel the request and set it to nil (since there's no reason to check the old username). If the request succeeds show up an icon, or whatever you want to do when you get the result from the server.
Edit:
Not sure if UIControlEventValueChanged
worked on previous versions of iOS or if it was a mistake from my side, but it doesn't seem to work any longer. UIControlEventEditingChanged
should be used instead.