【问题标题】:nsurlconnection in picker view in iOSiOS 选择器视图中的 nsurlconnection
【发布时间】:2016-04-26 06:00:48
【问题描述】:

我是 iOS 新手,我正在努力使用 nsurlconnection POST 方法在选取器视图中显示特定对象的值(id)。当我单击按钮时,它会使用 nslog 但不在选取器视图中显示“id”的所有值。

编码

按钮操作:

-(IBAction)sendDataUsingGet:(id)sender{

    [self sendDataToServer : @"GET"];
    [self.pickerdata reloadAllComponents];
    [self.view addSubview:pickerdata];

}

委托方法:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSMutableString *responseStringWithEncoded = [[NSMutableString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
    //NSLog(@"%@",responseStringWithEncoded  );// this nslog will display all d response....
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData: mutableData
                                                         options:kNilOptions
                                                           error:&error]; //Now we got top level dictionary


    arry = [json valueForKeyPath:@"BranchByList.id"];
    NSLog(@"%@",arry);   

    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    serverResponse.attributedText = attrStr;

} 

选择器视图:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;{
    return [arry count];
    //[self.pickerdata reloadAllComponents];
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;{

    return [[arry objectAtIndex:row] valueForKey:@"currency"];
    //[self.pickerdata reloadAllComponents];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
    NSLog([arry objectAtIndex:row]);

}

【问题讨论】:

  • 你不应该在新项目中使用 NSURLConnection,它已经被弃用了。

标签: ios objective-c nsurlconnection


【解决方案1】:

我认为您在数据从服务器到达之前重新加载选择器组件。

您应该从 viewdidload 调用 [self sendDataToServer : @"GET"]; this 而不是单击按钮,因此当用户按下按钮时,数据已经到达。我认为在您的情况下,选择器视图在数据来自服务器之前重新加载。

或者你可以把[self.pickerdata reloadAllComponents];这个语句放在connectionDidFinishLoading方法中作为最后一个语句。因此,只要有响应,它就会重新加载pickerview。你可以把这两条线都放进去

  [self.pickerdata reloadAllComponents];
  [self.view addSubview:pickerdata]; 

connectionDidFinishLoading 方法而不是buttonclick 方法。因此,当您单击按钮时,只会调用您的 get 方法,当响应到来时,它会重新加载选择器并显示以查看,但显示选择器需要一些时间。所以更好的方法是在加载 viewcontroller 时在 viewdidload 中调用 webservice,并在用户单击按钮之前以及当用户单击重新加载数据并显示选择器时使您的 array 准备就绪。

希望这会有所帮助:)

【讨论】:

  • 您到底遇到了什么问题?你能显示选择器视图(空)吗?或者你的选择器视图没有显示?
  • 选择器视图为空,单行 @Lion
  • 你设置了yourPickerView.delegate = selfyourPickerView.datasource = self 吗?并且在您的 .h 文件中,您是否符合 UIPickerViewDelegate 和 UIPickerViewDatasource 的协议?
  • 你能发布你从服务器获得的数组吗?
  • 并将braek点放在numberOfRowsInComponenttitleForRow方法中,并检查数组计数是什么,每次titleForRow返回的字符串是什么
【解决方案2】:

你应该在为数组分配新数据后调用reload data

-(IBAction)sendDataUsingGet:(id)sender{    
                [self sendDataToServer : @"GET"];
                //Show some loading views here 

         }


        -(void)connectionDidFinishLoading:(NSURLConnection *)connection
        {
            NSMutableString *responseStringWithEncoded = [[NSMutableString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
            //NSLog(@"%@",responseStringWithEncoded  );// this nslog will display all d response....
            NSError* error;
            NSDictionary* json = [NSJSONSerialization JSONObjectWithData: mutableData
                                                                 options:kNilOptions
                                                                   error:&error]; //Now we got top level dictionary


            arry = [json valueForKeyPath:@"BranchByList.id"];
            NSLog(@"%@",arry);   

            NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
            serverResponse.attributedText = attrStr;

        //Hidden loading views
        ...
        // Reloading data

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.view addSubview:pickerdata];
            [self.pickerdata reloadAllComponents];

        });

    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 2018-03-25
    • 2015-11-07
    相关资源
    最近更新 更多