将UITextfield整数值发布到Web服务器

时间:2014-08-19 10:17:09

标签: ios post webserver nsdictionary ios5.1

我有一个UITextField,因为我应该只能输入整数(没有文本),之后必须将整数值传递给NSMutableDictionary。然后从这本词典中我必须发布到网络服务器。

4 个答案:

答案 0 :(得分:3)

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"];

OR

//if you want to save as string
[myDictionary setObject:textField.text forKey:@"YourKey"];

答案 1 :(得分:3)

//创建文本字段

    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];

现在将您的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)

}     其他     {

[datadict setObject:[NSString stringWithFormat:@" "] forKey:@"txt"];

}

现在将值发布到web-server

   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);

答案 2 :(得分:2)

你可以尝试这样的事情 1.将textField'键盘类型'设置为数字键盘。

2.
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setValue:[NSNumber numberWithInt:[textField.text intValue]] forKey:@"Key"];

希望它有所帮助。

答案 3 :(得分:0)

这是执行此类操作的最佳方式,在 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;
}