UITextField - Dismiss keyboard when two digits have been entered

孤人 提交于 2019-12-30 12:25:11

问题


I have a UITextField, which uses a number pad to take input. How can I dismiss it/run a method call when two digits have been entered into the textField?


回答1:


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

   int count = [textField.text length];
    if(count>=2){

    [textField resignFirstResponder];
    }
return YES;
}



回答2:


Use the text field delegate method:

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

Here you can determine the proposed change to the text field - if the change is that the second character should be added, you can resign first responder. You should also return YES or directly set the text field's value so that the second character is updated.

Good point in the comments though - this might not be the best user experience!



来源:https://stackoverflow.com/questions/8203647/uitextfield-dismiss-keyboard-when-two-digits-have-been-entered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!