2017-03-29 42 views
0

enter image description here在IOS應用程序中我想開發UIPickerview,如this.code所示。單個UIViewcontroller中的MultipleUIPickerView問題

#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 *[email protected]「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 *[email protected]"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 *[email protected]"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 
      {[email protected]""; 
          } 
     } 
    } 
     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 
      { 
       [email protected]""; 

      } 

     } 
     } 
     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 
      { 
       [email protected]""; 
      } 

     } 
     } 
    } 


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

} 

使用上述代碼我想下發展像thissee全球化志願服務青年圖像,我有3 PickerView和數據被從肥皂WebService的設定,用於數據分配我有在歧陣列添加Indivisual SOAP響應,並且基於PickerView的標籤值數據分配,但問題是,只有第一個肥皂響應分配在pickerview其餘2是null,請建議我,如果我出錯了。

回答

0

你是在一個時間取而代之的是

[self dropdownshape]; 

[self dropdowncolor]; 

[self dropdownclarity]; 

調用3個的Web服務,您將首先撥打服務viewDidLoad中

  • (無效)viewDidLoad中{

    [ super viewDidLoad];

    [self dropdownshape];

}

之後首先建立連接中,可以調用第二web服務

如果(連接){

resshap = [NSMutableData數據];

[self dropdowncolor];

}

然後第二個連接建立的,您可以調用第三方Web服務

如果(連接){

rescolor = [NSMutableData data]; 

[self dropdownclarity]; 

} 

檢查以下鏈接它會幫助你

- (void)連接:(NSURLConnection *)連接didReceiveResponse:(NSURLResponse *)響應 {

if(connection == connection1) { 

    //Do something with connection 1 

} 

else if(connection == connection2) { 

    //Do something with connection 2  

} 

否則,如果(連接==連接3){

//Do something with connection 3 

} 

}

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

注:未測試

-1
Try 

picker1.delegate=self; 
picker1.tag = 1; 

picker2.delegate=self; 
picker2.tag = 2; 

picker3.delegate=self; 
picker3.tag = 3; 
+0

好吧,我試試這個 – Rucha

+0

它可以」 t解決我的問題 – Rucha

+0

@Rucha,PLZ檢查更新的答案。您缺少分配標籤。 –

相關問題