【问题标题】:how to POST UIPickerview Value to JSON url如何将 UIPickerview 值发布到 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”)

标签: ios objective-c json post uipickerview


【解决方案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 我该怎么做
猜你喜欢
  • 2016-06-05
  • 2014-10-17
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多