min*_*ieh 2 xcode uitextfield ipad
您好
我知道ipad键盘不喜欢iphone可以设置"UIKeyboardTypeNumberPad"!!
但如果我想它只能输入并在文本字段显示数字0到9.
如何比较文本字段中的用户键是否为数字?
预先感谢.
微型
而不是在显示后比较图形,而是在shouldChangeCharactersInRange中进行
一定要声明委托UITextFieldDelegate,以及我总是忘记的事情,确保textField本身的委托指向其中包含代码的类.
//---------------------------------------------------------------------------
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string length] == 0 && range.length > 0)
{
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return NO;
}
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
if ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0)return YES;
return NO;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7979 次 |
| 最近记录: |