开发者

UITextField jump to previous

I have two UITextField's in my view to let the user enter a zipcode in format 1234AA

  • UITextField 1 : for the number part with number pad
  • UITextField 2 : for the letters (regular key开发者_JAVA百科board)

I was able to automatically jump to the second textfield when the user has entered four digits.

I customized this method for that:

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

Now my question is, is there a way to automatically jump back from textfield 2 to textfield 1 when the users presses the delete key while textfield 2 is empty.

I couldn't get this working because when textfield 2 is empty the above method isn't called.

Does anyone know a way to do this?


Use textField.tag to identify your textFields. Example if you assign tags 0 and 1 for texFields 1 and 2 respectively.

// You could do this in the textFieldDidBeginEditing method //

If (textField.tag == 0)
   {
  //Now the first textField must be active//
  // do the method you had posted above//
    }

If (textField.tag ==1)
                   {
  //Now the second textField must be active//

    }

The logic behind checking for the keyboard events is provided in the link below. Alternatively you could also use NSNotification to check for keyboard events.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldChanged:)name:UITextFieldTextDidChangeNotification
        object:searchTextField];

Check this question on Stack Overflow for detecting BackSpace on Empty UITextField Detect backspace in UITextField

You could also use this tutorial to detect pressed keys and check for BackSpace and transfer control back to textField 1. http://www.cocos2d-iphone.org/forum/topic/11725

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜