Ios 将UITextfield整数值发布到web服务器
我有一个Ios 将UITextfield整数值发布到web服务器,ios,post,webserver,nsdictionary,ios5.1,Ios,Post,Webserver,Nsdictionary,Ios5.1,我有一个UITextField,因此我应该只能键入整数(没有文本),然后必须将整数值传递到NSMutableDictionary。然后我必须从这本词典发布到Web服务器。将UITextField的键盘类型更改为数字键盘,这样它只需要数字: [textField setKeyboardType:UIKeyboardTypeNumberPad]; 您还可以检查输入的文本是否为数字: - (BOOL)textField:(UITextField *)textField shouldChangeCha
UITextField
,因此我应该只能键入整数(没有文本),然后必须将整数值传递到NSMutableDictionary
。然后我必须从这本词典发布到Web服务器。将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;
}
- (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);
}
您可以使用以下代码将值保存在字典中:
//if you want to save as number
[myDictionary setObject:[NSNumber numberWithInt:[textField.text intValue] forKey:@"YourKey"];
或
你可以试试这样的东西
1.将文本字段“键盘类型”设置为数字键盘
2.
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setValue:[NSNumber numberWithInt:[textField.text intValue]] forKey:@"Key"];
希望有帮助。这是做这些事情的最好方法,在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;
}
//创建一个文本字段
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
{
if (string.length == 0 || [NSScanner scanInt:string]) {
return YES;
}
return NO;
}
- (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];
现在将textfield值传递给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)
}
其他的
{
现在将值发布到web服务器
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);
&如何将整数值传递给nsdictionary??