I am trying to capture when a user presses the del key on a number pad on an iPhone in a Edit Did Change event. I presume I can capture it through the sender event but can not find the object to grab and have looked on the net but can not find anywhere where I can get the key pressed through the sender event.
I am making a phone number text field formatter and would like to allow the user to use the del key to delete the “-“‘s. But now the way it is written of course it stops there. I just need to capture the del key and have it ignore the formatting.
Thoughts?
- (IBAction)homePhoneEditChanged:(id)sender
{
if(homePhone.text.length == 13)
{
homePhone.text = [homePhone.text substringToIndex:12];
return;
}
static BOOL toggle = NO;
if (toggle)
{
toggle = NO;
return;
}
// Add a condition here to ignore if the del key was pressed
if( homePhone.text.length == 3 || homePhone.text.length == 7)
{
// Add a -
toggle = YES;
homePhone.text = [NSString stringWithFormat:@"%@-",homePhone.text];
}
}
Phone number formatting is a fairly complex idea if you’re going to be concerned with localization. Would it be better to restrict the field to a number only field and then follow something like this?