将 UITextfield 整数值发布到 Web 服务器
Posted
技术标签:
【中文标题】将 UITextfield 整数值发布到 Web 服务器【英文标题】:Post UITextfield integer values to web server 【发布时间】:2014-08-19 10:17:09 【问题描述】:我有一个UITextField
,我应该只能输入整数(无文本),然后必须将整数值传递给NSMutableDictionary
。然后我必须从这本字典中发布到网络服务器。
【问题讨论】:
***.com/questions/12944789/… & ***.com/questions/10734959/… 如何将整数值传递给nsdictionary?? 【参考方案1】:将UITextField
的键盘类型更改为数字键盘,使其只输入数字:
[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"];
【讨论】:
【参考方案2】://创建一个文本字段
txt = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 150, 50)];
txt.placeholder = @"enter here";
txt.borderStyle = UITextBorderStyleRoundedRect;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
[txt setClearButtonMode:UITextFieldViewModeWhileEditing];
[txt setKeyboardType:UIKeyboardTypeNumberPad];
txt.delegate=self;
[cell.contentView addSubview:txt];
您可以检查输入的文本是否为数字:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
NSString *newString = [Height.text stringByReplacingCharactersInRange:range withString:string];
if([newString length] > 3)
UIAlertView *obj_AlertView=[[UIAlertView alloc]initWithTitle:@""
message:@"Enter 3 digit only"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[obj_AlertView show];
return !([newString length] > 3);
现在在您的按钮操作中创建一个 NSMutabledictionary 以发布
-(IBAction)btnClicked:(id)sender
UIButton *button = (UIButton *)sender;
button.tintColor=[UIColor redColor];
NSLog(@"%@",button);
NSMutableDictionary *datadict=[NSMutableDictionary dictionary];
现在将您的文本字段值传递给 nsmutabledictionary 以发布
if (txt.text !=NULL)
[datadict setValue:[NSNumber numberWithInt:[txt.text intValue]] forKey:@"txt"];
// [datadict setObject:[NSString stringWithFormat:@"%@",txt.text] forKey:@"txt"]; (if it is for NSdictionary)
别的
[datadict setObject:[NSString stringWithFormat:@" "] forKey:@"txt"];
现在将值发布到网络服务器
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:datadict options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *someURLSetBefore =[NSURL URLWithString:@" your post web-server link here"];
[request setURL:someURLSetBefore];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * string1=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",string1);
【讨论】:
【参考方案3】:你可以试试这样的 1. 将 textField 'Keyboard Type' 设置为 Number Pad。
2.
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setValue:[NSNumber numberWithInt:[textField.text intValue]] forKey:@"Key"];
希望对你有帮助。
【讨论】:
【参考方案4】:这是执行此类操作的最佳方法,请在 NSString 类别 中创建以下行,并根据您的需要检查验证。
-(NSString *)isTextFieldHasNumberOnly
NSString *strMessage;
NSString *enteredText=[self trimWhiteSpace];
if ([enteredText length]==0)
strMessage=@"Please enter number.";
return strMessage;
NSString *numbEx = @"^([0-9]+)?(\\.([0-9]1,2)?)?$";
NSRegularExpression *numbRegEx = [NSRegularExpression regularExpressionWithPattern:numbEx options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [numbRegEx numberOfMatchesInString:enteredText options:0 range:NSMakeRange(0, [enteredText length])];
if (numberOfMatches == 0)
strMessage=@"Please enter valid number.";
else
strMessage=@"";
return strMessage;
【讨论】:
以上是关于将 UITextfield 整数值发布到 Web 服务器的主要内容,如果未能解决你的问题,请参考以下文章
防止整数值在 ASP.NET Web API 模型绑定中设置布尔参数?
如何将 AVRO 文件中的整数值加载到 bigquery 中的日期列?