Search code examples
objective-csoapuipickerview

Issue with MultipleUIPickerView in single UIViewcontroller


enter image description here in IOS Application I want to develope UIPickerview like this.code is below.

#import "CalcViewController.h"

@interface CalcViewController ()

@end

@implementation EZCalcViewController

+(CalcViewController *)viewcontroller{
    CalcViewController *rz=[[CalcViewController alloc]initWithNibName:@"CalcViewController" bundle:nil];
    return  rz;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    picker1 = [[UIPickerView alloc] init];
    [self.view addSubview:picker1];
    picker2 = [[UIPickerView alloc] init];
    [self.view addSubview:picker2];
    picker3 = [[UIPickerView alloc] init];
    [self.view addSubview:picker3];
    picker1.delegate=self;
    picker1.delegate=self;
    _customPickerArrayshap = [[NSMutableArray alloc] init];
    _customPickerArraycolor = [[NSMutableArray alloc] init];
    _customPickerArrayclarity = [[NSMutableArray alloc] init];

   /// [_customPickerArrayshap.count = 0];
    [self dropdownshape];
    [self dropdowncolor];
    [self dropdownclarity];


}
-(void)dropdownshape
{


    NSLog(@"dropdown method is call");

    NSString *envalope=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                        "<soap:Body>\n"
                        "<value1 xmlns=\"http://tempuri.org/\">\n"
                        “</value1>\n"
                        "</soap:Body>\n"
                        "</soap:Envelope>"];

    envalope=[NSString stringWithFormat:envalope];
    NSData *envelope = [envalope dataUsingEncoding:NSUTF8StringEncoding];
    NSString *url=@“ABC.asmx";

    NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

    [request2 addValue:@"http://tempuri.org/Value1" forHTTPHeaderField:@"SOAPAction"];
    [request2 setHTTPMethod:@"POST"];
    [request2 setHTTPBody:envelope];
    [request2 setValue:@"text/xml; charset=utf-8"
    forHTTPHeaderField:@"Content-Type"];
    [request2 setValue:[NSString stringWithFormat:@"%d", [envelope length]]forHTTPHeaderField:@"Content-Length"];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request2 delegate:self];
    if (connection){
        resshap = [NSMutableData data];
        NSLog(@"connection Establish.");
    }
    else{
        NSLog(@"NSURLConnection initWithRequest: Failed to return a connection.");
    }

}
-(void)dropdowncolor
{


    NSLog(@"dropdown method is call");

    NSString *envalope=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                        "<soap:Body>\n"
                        "<value2 xmlns=\"http://tempuri.org/\">\n"
                        “</value2>\n"
                        "</soap:Body>\n"
                        "</soap:Envelope>"];

    envalope=[NSString stringWithFormat:envalope];
    NSData *envelope = [envalope dataUsingEncoding:NSUTF8StringEncoding];
    NSString *url=@"http://ABC.asmx";

    NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

    [request1 addValue:@"http://tempuri.org/value2" forHTTPHeaderField:@"SOAPAction"];
    [request1 setHTTPMethod:@"POST"];
    [request1 setHTTPBody:envelope];
    [request1 setValue:@"text/xml; charset=utf-8"
    forHTTPHeaderField:@"Content-Type"];
    [request1 setValue:[NSString stringWithFormat:@"%d", [envelope length]]forHTTPHeaderField:@"Content-Length"];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
    if (connection){
        rescolor = [NSMutableData data];
        NSLog(@"connection Establish.");
    }
    else{
        NSLog(@"NSURLConnection initWithRequest: Failed to return a connection.");
    }

}

-(void)dropdownclarity
{


    NSLog(@"dropdown method is call");

    NSString *envalope=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                        "<soap:Body>\n"
                        "<value3 xmlns=\"http://tempuri.org/\">\n"
                        “</value3>\n"
                        "</soap:Body>\n"
                        "</soap:Envelope>"];

    envalope=[NSString stringWithFormat:envalope];
    NSData *envelope = [envalope dataUsingEncoding:NSUTF8StringEncoding];
  NSString *url=@"http://ABC.asmx";

    NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

    [request1 addValue:@"http://tempuri.org/value3" forHTTPHeaderField:@"SOAPAction"];
    [request1 setHTTPMethod:@"POST"];
    [request1 setHTTPBody:envelope];
    [request1 setValue:@"text/xml; charset=utf-8"
    forHTTPHeaderField:@"Content-Type"];
    [request1 setValue:[NSString stringWithFormat:@"%d", [envelope length]]forHTTPHeaderField:@"Content-Length"];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
    if (connection){
       resclarity = [NSMutableData data];
        NSLog(@"connection Establish.");
    }
    else{
        NSLog(@"NSURLConnection initWithRequest: Failed to return a connection.");
    }

}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    [resshap setLength:0];
    [rescolor setLength:0];
     [resclarity setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

   [resshap appendData:data];
    [rescolor appendData:data];
    [resclarity appendData:data];

}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"connection didFailWithError: %@ %@", error.localizedDescription,
          [error.userInfo objectForKey:NSURLErrorFailingURLStringErrorKey]);
    NSString *err=error.localizedDescription;
    UIAlertView *alr=[[UIAlertView alloc]initWithTitle:@"App" message:err delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // [[ActivityIndicator currentIndicator]displayCompleted];
    NSLog(@"DONE. Received Bytes:%d",[resshap length]);
     NSLog(@"DONE. Received Bytes:%d",[rescolor length]);
     NSLog(@"DONE. Received Bytes:%d",[resclarity length]);

    NSString *theXml1 = [[NSString alloc] initWithBytes:[resshap mutableBytes] length:[resshap length] encoding:NSUTF8StringEncoding];
    NSLog(@"The final result 1--- :%@",theXml1);

    NSString *string1=[NSString stringWithFormat:@"r%@d",theXml1];
    NSLog(@"Object having value 1%@",string1);

      NSString *thexml2 = [[NSString alloc] initWithBytes:[rescolor mutableBytes] length:[rescolor length] encoding:NSUTF8StringEncoding];
    NSLog(@"The final result 2--- :%@",theXml1);

    NSString *string2=[NSString stringWithFormat:@"r%@d",thexml2];
    NSLog(@"Object having value  2%@",string2);

      NSString *thexml3 = [[NSString alloc] initWithBytes:[resclarity mutableBytes] length:[resclarity length] encoding:NSUTF8StringEncoding];
    NSLog(@"The final result 3--- :%@",theXml1);

    NSString *string3=[NSString stringWithFormat:@"r%@d",thexml3];
    NSLog(@"Object having value 3%@",string3);


    if(xmlParser1)
    {

    }
    xmlParser1 = [[NSXMLParser alloc] initWithData: resshap];
    [xmlParser1 setDelegate:self];
    [xmlParser1 setShouldResolveExternalEntities:YES];
    [xmlParser1 parse];
    //[theXml release];

    if(xmlParser2)
    {

    }
    xmlParser2 = [[NSXMLParser alloc] initWithData: rescolor];
    [xmlParser2 setDelegate:self];
    [xmlParser2 setShouldResolveExternalEntities:YES];
    [xmlParser2 parse];
    //[theXml release];

    if(xmlParser3)
    {

    }
    xmlParser3 = [[NSXMLParser alloc] initWithData: resclarity];
    [xmlParser3 setDelegate:self];
    [xmlParser3 setShouldResolveExternalEntities:YES];
    [xmlParser3 parse];
    //[theXml release];


}


//}
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *)attributeDict {
    if( [elementName isEqualToString:@“Value1Result"])
    {
        if (!soapResults1)
        {
            soapResults1 = [[NSMutableString alloc] init];
        }
        elementFound1 = YES;
    }
    else if([elementName isEqualToString:@“Value2Result"]){
        if (!soapResults2)
        {
            soapResults2 = [[NSMutableString alloc] init];
        }
        elementFound2 = YES;
    }else if( [elementName isEqualToString:@“value3Result"])
    {
        if (!soapResults3)
        {
            soapResults3 = [[NSMutableString alloc] init];
        }
        elementFound3 = YES;
    }


}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
    if (elementFound1)
    {
        [soapResults1 appendString: string];



        NSArray * jsonarr1 =[NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        NSLog(@"print array :%@",jsonarr1);
        for (NSDictionary * d in jsonarr1)
        {
            if ([d objectForKey:@"SHP"]!=[NSNull null])
            {
                [picker1 reloadAllComponents];
                self.shapestring=[NSString stringWithFormat:@"%@",[d objectForKey:@“V1”]];
                 NSLog(@"pVALUE FOR CLR :%@",self.shapestring);
                [_customPickerArrayshap addObject:self.shapestring];
               NSLog(@"pVALUE AFTER ARRAY FILL:%d",_customPickerArrayshap.count);
                [picker1 reloadAllComponents];
            }
            else
            {self.shapestring=@"";
                            }
        }
    }
        if(elementFound2){
            [soapResults2 appendString: string];



            NSArray * jsonarr2 =[NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
            NSLog(@"print array :%@",jsonarr2);
            for (NSDictionary * d in jsonarr2)
            {

            if ([d objectForKey:@"RAPCOL"]!=[NSNull null])
            {
                [picker2 reloadAllComponents];
                self.colorstring=[NSString stringWithFormat:@"%@",[d objectForKey:@“v2”]];
                NSLog(@"pVALUE FOR CLR :%@",self.colorstring);
                [_customPickerArraycolor addObject:self.colorstring];
                // NSLog(@"pVALUE AFTER ARRAY FILL:%d",_customPickerArray1.);
                [picker3 reloadAllComponents];
            }
            else
            {
                self.colorstring=@"";

            }

        }
        }
        if(elementFound3){
            [soapResults3 appendString: string];

            NSArray * jsonarr3 =[NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
            NSLog(@"print array :%@",jsonarr3);

            for (NSDictionary * d in jsonarr3)
            {

            if ([d objectForKey:@"CLR"]!=[NSNull null])
            {
                [picker3 reloadAllComponents];
                self.claritystring=[NSString stringWithFormat:@"%@",[d objectForKey:@“V3”]];
                NSLog(@"pVALUE FOR CLR :%@",self.claritystring);
                [_customPickerArrayclarity addObject:self.claritystring];
                // NSLog(@"pVALUE AFTER ARRAY FILL:%d",_customPickerArray1.);
                [picker3 reloadAllComponents];
            }
            else
            {
                 self.claritystring=@"";
            }

        }
        }
    }


-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@“value1Result"])
    {


        [self.customPickerArrayshap addObject:@“v1”];

        NSLog(@"%@",soapResults1);

        [soapResults1 setString:@""];
        elementFound1 = FALSE;

    }
   else if ([elementName isEqualToString:@“Value2Result"])
    {


        [self.customPickerArraycolor addObject:@“v2”];
             [soapResults2 setString:@""];
        elementFound2 = FALSE;

    }else if ([elementName isEqualToString:@“Value3Result"]){
        [self.customPickerArrayclarity addObject:@“V3”];
             [soapResults3 setString:@""];
        elementFound3 = FALSE;
    }



}
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    if([pickerView tag]==1)
    {
        return _customPickerArrayshap.count;
    }
    else if([pickerView tag]==2)
    {

        return _customPickerArraycolor.count;
    }else if([pickerView tag]==3){


        return _customPickerArrayclarity.count;
    }


}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if([pickerView tag]==1)
    {

        return _customPickerArrayshap[row];
    }
    else if([pickerView tag]==2)
    {
        return _customPickerArraycolor[row];
    }else if([pickerView tag]==3){
        return _customPickerArrayclarity[row];
    }

}

using above code i want to Develope like thissee referance image,I have 3 PickerView and data is set from soap WebService,for data assign i have add Indivisual soap response in in differant array,and based on PickerView's tag value data is assign,but problem is that only first soap response is assign in pickerview remaining 2 are null,please suggest me if i'm go wrong.


Solution

  • You are calling 3 web services at a time

    [self dropdownshape];
    
    [self dropdowncolor];
    
    [self dropdownclarity];
    

    Instead of that you will call first service in viewDidLoad

    • (void)viewDidLoad {

      [super viewDidLoad];

      [self dropdownshape];

    }

    after that First connection is Established in that you can call second web service

    if (connection){

    resshap = [NSMutableData data];

    [self dropdowncolor];

    }

    Then Second connection is Established in that you can call third web service

    if (connection){

    rescolor = [NSMutableData data];
    
    [self dropdownclarity];
    
    }
    

    Check below link it will helpful to you

    -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response {

    if(connection == connection1) {
    
        //Do something with connection 1
    
    }
    
    else if(connection == connection2) {
    
        //Do something with connection 2    
    
    }
    

    else if(connection == connection3) {

        //Do something with connection 3
    
    }
    

    }

    https://stackoverflow.com/a/6434433/5184217

    NOTE: Not Tested