2014-07-21 3 views
-1

나는 uipicker 메서드를 호출하는 데 문제가 있음을 알려주고 있지만 가능한 한 빨리 나를 도와주세요.메서드가 다른 클래스에서 호출하지 않습니다.

나는 당신에게 내 프로젝트 uipicker의 deligae 방법에 대한 설명을 제공하고 사전에 덕분에 당신은

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler: 
      ^(NSURLResponse *response, NSData *data, NSError *connectionError) { 

       if (!connectionError) { 
    //enter code here 
        } 
})]; 

를 사용하여 응답을 가져올 수 demosecondcontroller.m

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     StateList*objstatelist= [[StateList alloc]init]; 
     [objstatelist callingStatelist]; 
    }); 
}); 
StateList.m(nsobject) 
-(void)callingStatelist{ 

    @try { 
     CheckConnection*obj=[[CheckConnection alloc]init]; 
     UrlService*objurlservice= [[UrlService alloc]init]; 
     [objurlservice url]; 

     [obj internet ]; 
     if (obj.conection==true) { 
      NSString* soapMessage= @"<?xml version='1.0' encoding='utf-8'?> <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/'><soap:Body><GetStateList xmlns='http://tempuri.org'><webreq>%@</webreq></GetStateList></soap:Body></soap:Envelope>"; 
      // create a url to your asp.net web service. 
      NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"%@",objurlservice.urlstring]]; 

      // create a request to your asp.net web service. 
      NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl]; 

      // add http content type - to your request 
      [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

      // add SOAPAction - webMethod that is going to be called 
      [theRequest addValue:@"http://tempuri.org/GetStateList" forHTTPHeaderField:@"SOAPAction"]; 

      // count your soap message lenght - which is required to be added in your request 
      NSString *msgLength=[NSString stringWithFormat:@"%lu",(unsigned long)[soapMessage length]]; 
      // add content length 
      [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 

      // set method - post 
      [theRequest setHTTPMethod:@"POST"]; 
      // set http request - body 
      [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

      // establish connection with your request & here delegate is self, so you need to implement connection's methods 
      NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

      // if connection is established 
      if(con) 
      { 
       responseData=[[NSMutableData data] init]; 
       // here -> NSMutableData *myWebData; -> declared in .h file 
      } 
     } 
     else{ 
      UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Smart24x7" message:@"Check your internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
      [alert show]; 
     } 
    } 
    @catch (NSException *exception) { 
     NSLog(@"exeption%@",exception); 
    } 
} 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    responseData = [[NSMutableData alloc]init]; 
} 

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

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

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    responseData = nil; 

    responseString = [responseString stringByReplacingOccurrencesOfString:@"&gt;" 
                   withString:@">"]; 
    responseString = [responseString stringByReplacingOccurrencesOfString:@"&lt;" 
                   withString:@"<"]; 
    NSData* data = [responseString dataUsingEncoding:NSUTF8StringEncoding]; 
    NSLog(@"response%@",responseString); 
    responcearray = [[NSMutableArray alloc]init]; 

    parsar = [[NSXMLParser alloc] initWithData: data]; 
    [parsar setDelegate: (id)self]; 
    [parsar setShouldResolveExternalEntities:YES]; 
    [parsar parse]; 


} 


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict 
{ 
    elementValue= elementName; 

    if ([elementValue isEqualToString:@"item"]) 
    { 

     //  elementValue = [[NSMutableString alloc] init]; 
    } 

} 
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    [responcearray addObject:string]; 
} 

- (void)parserDidEndDocument:(NSXMLParser *)parser 
{ 
    DEMOSecondViewController*obj=[[DEMOSecondViewController alloc]init]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      [obj get_Statelist_Responce:responcearray]; 
     }); 
    }); 

} 
//in demosecondcontroller.m 
-(void)get_Statelist_Responce:(NSArray*)responce_ArrayForstate 
{ 
    stateListarray= [[NSMutableArray alloc]init]; 
    stateListarray=responce_ArrayForstate; 
    [self CreateTable]; 
} 

-(void)CreateTable 
{ 
    pickerView.hidden=YES; 
    done.hidden=YES; 
    doneclicked.hidden=YES; 
    pickerView = [[UIPickerView alloc] init]; 
    [pickerView setDataSource: (id)self]; 
    [pickerView setDelegate: (id)self]; 
    [pickerView.layer setBorderWidth:2.0]; 
    pickerView.layer.borderColor = [UIColor grayColor].CGColor; 
    float screenWidth = [UIScreen mainScreen].bounds.size.height; 
    pickerView.layer.cornerRadius=5; // Set the picker's frame. We set the y coordinate to 50px. 
    [pickerView setFrame: CGRectMake(0.0, screenWidth-200, 320.0, 180)]; 
    pickerView.showsSelectionIndicator = YES; 
    [pickerView setBackgroundColor:[UIColor whiteColor]]; 
    // OK, we are ready. Add the picker in our view. 
    [self.view addSubview: pickerView]; 
    [cityalert dismissWithClickedButtonIndex:0 animated:YES]; 
    [statealert dismissWithClickedButtonIndex:0 animated:YES]; 
    doneclicked = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    doneclicked .backgroundColor= [UIColor blackColor]; 
    [doneclicked setFrame:CGRectMake(0, screenWidth-200, 320, 30)]; 
    [doneclicked addTarget:self action:@selector(doneClickedpicker) forControlEvents:UIControlEventTouchUpInside]; 
    [doneclicked setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    doneclicked.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    [self.view addSubview:doneclicked]; 
    [doneclicked setTitle:@"Done" forState:UIControlStateNormal]; 
    doneclicked.contentEdgeInsets = UIEdgeInsetsMake(0, 270, 0, 0); 
    _datePickerView.hidden=YES; 
    done.hidden=YES; 
} 
// not called these method 
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 1; 
} 
    // please help me 
+0

에 의해

-(void)callingStatelist{ // code } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ responseData = [[NSMutableData alloc]init]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [responseData appendData:data]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ } -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ } 

방법을 대체 할 수있는 [더 나은 질문을하는 방법] (http://stackoverflow.com/ 도움/how-to-ask). – veovo

답변

0

나에게
도와주세요 를 호출되지 않습니다 비동기 스레드에서 해당 웹 서비스에 대한.

코드에서 스레드에서 urlconnection을 호출하고 UI 스레드에 위임하면 문제가 발생합니다. 비동기 호출의 경우 위의 메서드를 사용하고 UI/기본 스레드에서 해당 웹 서비스 클래스를 호출합니다. 도움이 될 수 있습니다.

편집 ***** 당신은 읽어 보시기 바랍니다

-(void)callingStatelist{ 

     @try { 
      CheckConnection*obj=[[CheckConnection alloc]init]; 
      UrlService*objurlservice= [[UrlService alloc]init]; 
      [objurlservice url]; 

      [obj internet ]; 
      if (obj.conection==true) { 
       NSString* soapMessage= @"<?xml version='1.0' encoding='utf-8'?> <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/'><soap:Body><GetStateList xmlns='http://tempuri.org'><webreq>%@</webreq></GetStateList></soap:Body></soap:Envelope>"; 
       // create a url to your asp.net web service. 
       NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"%@",objurlservice.urlstring]]; 

       // create a request to your asp.net web service. 
       NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl]; 

       // add http content type - to your request 
       [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

       // add SOAPAction - webMethod that is going to be called 
       [theRequest addValue:@"http://tempuri.org/GetStateList" forHTTPHeaderField:@"SOAPAction"]; 

       // count your soap message lenght - which is required to be added in your request 
       NSString *msgLength=[NSString stringWithFormat:@"%lu",(unsigned long)[soapMessage length]]; 
       // add content length 
       [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 

       // set method - post 
       [theRequest setHTTPMethod:@"POST"]; 
       // set http request - body 
       [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

       // establish connection with your request & here delegate is self, so you need to implement connection's methods 
       NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 


       [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler: 
       ^(NSURLResponse *response, NSData *data, NSError *connectionError) { 

        if (!connectionError) { 

         NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
         data = nil; 

         responseString = [responseString stringByReplacingOccurrencesOfString:@"&gt;" 
                        withString:@">"]; 
         responseString = [responseString stringByReplacingOccurrencesOfString:@"&lt;" 
                        withString:@"<"]; 
         NSData* data = [responseString dataUsingEncoding:NSUTF8StringEncoding]; 
         NSLog(@"response%@",responseString); 
         responcearray = [[NSMutableArray alloc]init]; 

         parsar = [[NSXMLParser alloc] initWithData: data]; 
         [parsar setDelegate: (id)self]; 
         [parsar setShouldResolveExternalEntities:YES]; 
         [parsar parse]; 

        } 
       })]; 
      } 
      else{ 
       UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Smart24x7" message:@"Check your internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
       [alert show]; 
      } 
     } 
     @catch (NSException *exception) { 
      NSLog(@"exeption%@",exception); 
     } 

    } 
+0

@ esha 여기서이 코드를 사용할 수 있습니다. – Shashank

+0

현재 NSURLConnection * con = [[NSURLConnection alloc] initWithRequest : theRequest delegate : self];를 사용하고 있습니다. if (con) { responseData = [[NSMutableData data] init]; // 여기에 -> NSMutableData * myWebData; -> .h 파일에 선언 됨} ' 메소드. 오히려 위의 메소드를 사용하여 웹 서비스 응답을 얻을 수 있습니다. 디스패치 비동기에서 작동합니다. – Esha

+0

실제로 피커 생성 방법이 호출 될 때 컴파일러는 자신의 호출 상태 인 – Shashank

관련 문제