Search code examples
iosobjective-cpostnsurlconnectionuipickerview

picker view empty after response in iOS


.h file i am new to iOS i am passing url link using POST method after getting response i am unable to display in picker view.

method for 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);
    }

Delegate method for picker view:

-(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]);

}

i am getting response and displayed in nslog but in picker view its showing empty with single line.I don't no where its going wrong please help me.pickerview datasource and delegate connection


Solution

  • Create a Outlet of picker view and reload it in connectionDidFinishLoading

    [picker reloadAllComponents];