方法不是从另一个类调用

时间:2014-07-21 07:14:46

标签: ios delegates uipickerview

我给你的代码我有问题调用uipicker的方法,但没有调用请尽快帮助我。 提前谢谢

我给你描述我的项目uipicker deligae方法没有被调用         请帮帮我 在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 

1 个答案:

答案 0 :(得分:0)

您可以使用

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

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

并从异步线程获取该Web服务的响应。

在您的代码中,您在UI线程上调用了线程和委托的urlconnection,因此这就产生了问题。对于异步调用使用上面的方法,从UI /主线程调用该Web服务类。 可能这可以帮到你。

修改***** 你可以替换

-(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{
}

方法

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

    }