Iphone NumberPad限制数字输入

Iphone NumberPad限制数字输入,iphone,cocoa,Iphone,Cocoa,我有一些简单的UITextFields来显示Numberpad 我想限制用户对输入的控制 例如,一个特定的文本字段不允许任何超过32的值 您可以使用使用哪个参数 [aTextfield.text intValue] < 32 [aTextfield.text intValue]

我有一些简单的UITextFields来显示Numberpad

我想限制用户对输入的控制

例如,一个特定的文本字段不允许任何超过32的值

您可以使用使用哪个参数

[aTextfield.text intValue] < 32
[aTextfield.text intValue]<32

将文本字段中的文本与32进行比较。;-)

我将实现此UITextFieldDelegate方法:

- (BOOL) textField: (UITextField *) textField shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString *) string
{
    NSString *result = [textField.text stringByReplacingCharactersInRange: range withString: string];
    if ( [result intValue] < 0 || [result intValue] > 32 )
        return NO;
    return YES;
}
-(BOOL)textField:(UITextField*)textField应更改字符范围:(NSRange)范围替换字符串:(NSString*)字符串
{
NSString*result=[textField.text stringByReplacingCharactersInRange:range with string:string];
如果([result intValue]<0 | |[result intValue]>32)
返回否;
返回YES;
}

为了保持一致性,您可能还需要实现-textfieldshouldenediting:在值无效时返回false。

谢谢Sandro。这会限制用户输入超过32的值吗?不,这只是比较文本字段中的值。但当他试图结束编辑时,你可以显示一条消息…;-)干杯,科斯蒂克。我稍后再查。