我有一个 UITextField
,我应该只能输入整数(无文本),然后必须将整数值传递给 NSMutableDictionary
。然后我必须从这本词典发布到网络服务器。
最佳答案
将 UITextField
的键盘类型更改为 Number Pad,以便它只接受数字:
[textField setKeyboardType:UIKeyboardTypeNumberPad];
您还可以检查输入的文本是否为数字:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (string.length == 0 || [NSScanner scanInt:string]) {
return YES;
}
return NO;
}
您可以使用以下代码将值保存在字典中:
//if you want to save as number
[myDictionary setObject:[NSNumber numberWithInt:[textField.text intValue] forKey:@"YourKey"];
或
//if you want to save as string
[myDictionary setObject:textField.text forKey:@"YourKey"];
关于ios - 将 UITextfield 整数值发布到 Web 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25380887/