- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([newString length]>=1)
{
NSString *sepStr=[newString substringToIndex:1];
return !([sepStr length]>1);
}
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;}
- (IBAction)btnc:(id)sender {
if([_textfield.text length]>2)
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"WARNING" message:@"Please Fill The Mendatory Fields." preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
UIAlertAction* OK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
}];
[alert addAction:OK];
}
else{
desViewController *face=[self.storyboard instantiateViewControllerWithIdentifier:@"REar"];
[self.navigationController pushViewController:face animated:YES];
}
我有一个应用,我想在UITextField
中输入一个数值。但是我想只允许输入2位数字,而只允许输入1到31位数字。知道我怎么能修改它只接受限制在2位数和1到31位的数值?
答案 0 :(得分:0)
你走了 创建仅支持2个数字值的文本字段:
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
if (textField== _textfield) {
int MAXLENGTH=2;
NSString *newText = [_textfield.text stringByReplacingCharactersInRange:range withString:string];
if(newText.length>MAXLENGTH) {
return NO;
}
return YES;
}
return YES;
}
仅限1到31(在目标C中),您可以检查如下验证:=
if([_textfield.text intValue]<=31 && [_textfield.text intValue]>0)
{
[_textfield resignFirstResponder];
//Your Code
}
答案 1 :(得分:0)
试试这个:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField.text.length == 0 && [string isEqual: @" "] )
{
return NO;
}
else
{
NSString* str = [NSString stringWithFormat:@"%@%@",textField.text,string];
NSNumber* value = nil;
NSNumberFormatter* format = [[NSNumberFormatter alloc] init];
value = [format numberFromString:str];//returns and
if (value == nil){
//do something useful i.e. show alert
return NO;
}
else if (value.intValue > 31)
{
return NO;
}
else
{
return YES;
}
}
return YES;
}
答案 2 :(得分:0)
首先将keybordType设置为NumberPad,以便您的UITextField不支持除数字之外的任何内容。然后让你的UITextField听取其中的编辑事件。
[mTextField setKeyboardType:UIKeyboardTypeNumberPad];
[mTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents: UIControlEventEditingChanged];
现在&#39; textFieldDidChange&#39;方法应该是这样的。除了1到31之间的数字之外,它不会接受任何输入。
- (void) textFieldDidChange : (id) sender {
NSString *text = [mTextField text];
if (text == nil) {
return;
}
int value = text.intValue ;
if (value < 1) {
[mTextField setText:@""];
return;
}
if (value > 31) {
NSString * val = [NSString stringWithFormat:@"%d", value];
val = [val substringToIndex: (val.length - 1)];
[mTextField setText:val];
}
}
我希望这肯定会有所帮助。
答案 3 :(得分:-1)
让你的逻辑简单明了。
让textField
仅允许Number Type
输入:
self.yourTextField.keyboardType = UIKeyboardTypeNumberPad;
然后在-textField:shouldChangeCharactersInRange:replacementString:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *str = [NSString stringWithFormat:@"%@%@", textField.text, string];
long intValue = str.integerValue;
if (intValue < 1 || intValue > 31) {
self.yourTextField.text = textField.text;
return NO;
}
return YES;
}