iOS中的响应后选择器视图为空

时间:2016-04-27 09:48:30

标签: ios objective-c post nsurlconnection uipickerview

.h file 我是iOS新手我在获取响应后使用POST方法传递url链接我无法在选择器视图中显示。

POST的方法:

    -(void) sendDataToServer : (NSString *) method{
        NSString *Branchid=@"3";

        NSURL *url = nil;
        NSMutableURLRequest *request = nil;
        if([method isEqualToString:@"GET"]){

            NSString *getURL = [NSString stringWithFormat:@"%@?branch_id=%@", URL, Branchid];
            url = [NSURL URLWithString: getURL];
            request = [NSMutableURLRequest requestWithURL:url];
            NSLog(@"%@",getURL);

        }else{  // POST

            NSString *parameter = [NSString stringWithFormat:@"branch_id=%@",Branchid];
            NSData *parameterData = [parameter dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

            url = [NSURL URLWithString: URL];
            NSLog(@"%@", parameterData);
            request = [NSMutableURLRequest requestWithURL:url];
            [request setHTTPBody:parameterData];

            arr= [NSMutableString stringWithUTF8String:[parameterData bytes]];
        }

        [request setHTTPMethod:method];
        [request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        if( connection )
        {
            mutableData = [NSMutableData new];
            }

    }

Delegate method for  POST:


-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    [mutableData setLength:0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mutableData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
        return;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


    NSError* error;
    json = [NSJSONSerialization JSONObjectWithData: mutableData
                                           options:kNilOptions
                                             error:&error];

    responseArray = [json valueForKeyPath:@"BranchByList.currency"];
    NSLog(@"%@",responseArray);
    }

选择器视图的委托方法:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;{
        return [responseArray count];
    }

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;{
    return [responseArray objectAtIndex:row];
}

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

}

我收到回复并显示在nslog中,但在选择器视图中显示为空单行。我不知道哪里出错了请帮帮我。pickerview datasource and delegate connection

1 个答案:

答案 0 :(得分:0)

创建一个选择器视图出口并在connectionDidFinishLoading

中重新加载

[picker reloadAllComponents];