将字符串格式的整数值传递给POST方法

时间:2016-05-11 05:13:50

标签: ios objective-c uitextfield

我是IOS的新手,我创建了一个名为(value1)和string为(fourthstr)的文本字段。当用户在textfield中输入整数值时,该值可以存储在字符串中,最后该字符串可以作为POST方法的参数传递

编码: viewdidload

fourthstr = [NSString stringWithFormat:@"%d", value1.text];

但字符串没有传递给post方法。

发布方法:

-(void) sendDataToServer : (NSString *) method params:(NSString *)str{


    NSData *postData = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[str length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];


    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];

    if( theConnection ){

        mutableData = [[NSMutableData alloc]init];
    }
}

选择器视图委托:

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{
    if(pickerView.tag ==2){
    txtText.text = (NSString *)[arrMsg objectAtIndex:row];
        branchid1 = (NSString *)[arrmsg1 objectAtIndex:row];

        NSLog([arrmsg1 objectAtIndex:row]);

    }else if(pickerView.tag ==1){
    currency1.text = (NSString *)[currencyname1 objectAtIndex:row];
        currencyid1 = (NSString *)[id1 objectAtIndex:row];
        NSLog([id1 objectAtIndex:row]);


    }
    else
    {
        currency2.text = (NSString *)[from_currency objectAtIndex:row];
        currencyid2 = (NSString *)[id2 objectAtIndex:row];

        NSLog(@"%@",currencyid2);
        NSLog([id2 objectAtIndex:row]);
    }
        str = [NSString stringWithFormat:@"branch_id=%@&from_curr=%@&to_curr=%@&value=%@",branchid1,currencyid1,currencyid2,fourthstr];
[self sendDataToServer :@"POST" params:str];

3 个答案:

答案 0 :(得分:0)

//文本字段文本属性始终给出字符串值

fourthstr =  value1.text;

答案 1 :(得分:0)

使用此代码,

首先将textfield值分配给string,

NSString *fourthstr = value1.text;

参数赋值如下,

NSDictionary *queryParams = @{
                                  @"YourKeyValue"          : fourthstr,
                                  };

然后上面的param传递给post方法,希望它有用

答案 2 :(得分:0)

value1.text将在viewdidload上清空字符串(UITextfield的默认值'文本属性@""),将它传递给POST方法的参数时应该使用它。

相关问题