iOS UITextField : Any way to detect the Delete key event when the field is empty ?

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
I'm trying to do something similar to the Mail application on Iphone : when you add a recipient, if you hit the Delete key when the field is empty, then the cursor disappears and then bubble will be highlighted.

Any idea on how we can capture the Delete key event when the text field is empty ? Or if there's no way, then do you have any idea how the Mail application can work like that ?

Thanks.
 

drf1229

macrumors regular
Jun 22, 2009
237
0
I'm trying to do something similar to the Mail application on Iphone : when you add a recipient, if you hit the Delete key when the field is empty, then the cursor disappears and then bubble will be highlighted.

Any idea on how we can capture the Delete key event when the text field is empty ? Or if there's no way, then do you have any idea how the Mail application can work like that ?

Thanks.
I'm not sure if this is possible through one action, however, I have a suggestion:
Make an IBAction (textfieldDidBeginEditing or w.e its called) and try something like this:
Code:
-(void)viewDidLoad{
//Txt will be an NSString called in your header file
txt=textfield.text;


}
-(IBAction)textFieldDidBeginEditing{
if ([txt length]>textfield.text){
//Your action

}
txt=textfield.text;
}
I'm not sure how accurate this is but it just might work.
 
Comment

firewood

macrumors 604
Jul 29, 2003
7,735
956
Silicon Valley
Put a leading space character in the textfield. Delete it before you use it in the email header. Use the delegate method to allow each change to the textfield, but check the size each call. If the number of characters in the textfield ever goes to zero, you know the user has either hit the delete key, or cut the entire field.
 
Comment

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
@drf1229 : i think it can not capture the event

@firewood : i did try this but it has some unexpected effect. If you tap the text field for a few times, you will see the cursor jumps back and forth between the current position and zero position (leftmost position). Then sometimes we can not capture the delete key event. I did go arround this by putting any non-space character and it works, but it's not what i want.

Also in the Mail application, when you tap the text field, it only pop up "Paste" button, that means the text field is really emtpy. If we put leading space character, it will aslo pop up "Select", "Select all".
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.