如何将 UIPickerview 值发布到 JSON url

Posted

技术标签:

【中文标题】如何将 UIPickerview 值发布到 JSON url【英文标题】:how to POST UIPickerview Value to JSON url 【发布时间】:2014-07-29 06:29:40 【问题描述】:

我有两个 UITextFields(cityNames,collageNames)。我获取 JSON url 的数据。我根据 UITextFields 获取并放置数据到 UIPickerview。 when select cityName that city name placed into UITextFields but now I need that particular index value post to JSON URL and same as collage name .我知道字符串值 Post to JSON url。但我不知道如何将UIPickerview 索引值发布到 JSON url。

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

      NSLog(@"select row is %d",row);
    if(pickerView.tag==1)
     CityName.text = [secondArray objectAtIndex:row];

        string1=
        [NSString stringWithFormat:@"%d",[yourpicker selectedRowInComponent:0]];
        NSLog(@"string ring %@",string1);

           
    else if(pickerView.tag==2)
     CollageName.text = [firstArray objectAtIndex:row];

        string2=
        [NSString stringWithFormat:@"%d",[yourpicker selectedRowInComponent:0]];

    

String1 和 String2 是获取 IndexValue 。但我不知道如何将特定的 Stringvalue 传递给 JSON。我很累,但是当我使用 Did-select 方法的 NSLog outSide 打印字符串 1 和 2 时,它显示空值。当我打印Didselect 方法中的字符串值显示选择索引值

 NSString *post =[NSString stringWithFormat:@"?&citynames=%@&collageNames=%@",cityname.text,collagename.text];

 NSString *post =[NSString stringWithFormat:@"?&citynames=%@&collageNames=%@",string1,string2];


    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;

    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.3.35:8090/SaveDollar/rest/users/add"]]];


    NSLog(@"getData%@",request);

    [request setHTTPMethod:@"POST"];

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];

    [request setHTTPBody:postData];

    NSLog(@"getData%@",request);


    con3 = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(con3)
          webData3=[NSMutableData data];
        NSLog(@"Connection successfull");
        NSLog(@"GOOD Day My data %@",webData3);
    
    else
    
        NSLog(@"connection could not be made");
    

我像这样累了(上面的代码)。我没有得到。所以请告诉我如何将UIPickerView indexValue 发布到 JSON 网址。

感谢高级。

【问题讨论】:

您要发布索引(例如“3”)还是实际值(例如“Chandigarh University”)? @MichaelDautermann 是的 @MichaelDautermann 在 textFiled 中显示“昌迪加尔大学”,但发布该号码请给我任何想法 @MichaelDautermann 请给我任何想法。我需要在 TextFeilds 中显示(例如“昌迪加尔大学”),但像这样发布该索引(例如“3”) 【参考方案1】:

您有两个不同的pickerViews,所以没问题。

从两个不同的数组中获取实际值:

    NSString *collegeName=[collegeArray objectAtIndex:[self.dataPicker1 selectedRowInComponent:0]];

或者:

如果您能够看到 textField 值,您也可以将它们用作:

NSString *collegeName=collegeTextField.text;

另一种获取索引的方法,即从 textField:

int index=[collegeNameArray indexOfObject:collegeName];

然后你可以将index 传递给你的urlString。

要获取 pickerView 选定的索引,请使用:

[self.dataPicker1 selectedRowInComponent:0]

您可以将 urlString 构建为:

urlString=[NSString stringWithFormat:@"?&citynames=%@&collageNames=%@",[self.dataPicker1 selectedRowInComponent:0],[self.dataPicker2 selectedRowInComponent:0]];

【讨论】:

感谢您的回复,我需要。我需要在 TextFeilds 中显示(例如“昌迪加尔大学”),但是像这样发布索引(例如“3”)所以请给我任何想法 答案的最后一行给你索引 我知道,但请提前我是新手请解释如何将该索引值传递给 URL 我试过这样 string1= [NSString stringWithFormat:@"%d",[yourpicker selectedRowInComponent:0]]; NSLog(@"字符串环 %@",string1);但是当我在 Didselect 方法之外打印 String 1 时,它显示空值 感谢回复但我只使用了一个 UIPickerview 我该怎么做

以上是关于如何将 UIPickerview 值发布到 JSON url的主要内容,如果未能解决你的问题,请参考以下文章

如何根据标签显示 UIPickerView 的选定值

UIPickerView 和 UITextField 从 UIPickerView 获取值

使用工具栏按钮更改 UIPickerView 值

如何将可点击的图像添加到 UIPickerView 行?

如何在 iOS Swift 3 中滚动到 UIPickerView 中的特定行

如何将列添加到预先存在的 uipickerview