iOS:将解析后的值从NSObject类中的soap webservice传递回UIViewController

时间:2013-05-03 11:47:03

标签: ios xcode web-services soap nsobject

我不知道为什么这么困难但是我无法让它发挥作用。这是我的简单问题:

我有UIViewController调用执行解析作业的NSObject类的paring类类型。

我只需要将解析后的数据值返回给ViewController

任何机构能否为我提供一个简单的步骤或任何建议来做到这一点...... 我真的需要为我的项目解决这个问题,截止日期已接近......

由于 这是我的代码:

ViewController.m

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    wsClass *ws = [[wsClass alloc] init];
    [ws initParsing];
}

wsClass.h

@interface ProjListClass : NSObject

@property(nonatomic,strong) NSString * PROJECT_ID;
@property(nonatomic,strong) NSString * SHORT_DESCR;
@property(nonatomic,strong) NSString * LOCATION;
@end;

@interface wsClass : NSObject <NSXMLParserDelegate>{
ProjListClass *proj_obj;
ProjListClass *prj;

NSMutableData* webData;
NSMutableString *soapResults;
NSURLConnection *conn;

NSXMLParser *xmlParser;

}
@property(nonatomic,strong) NSMutableArray * arrHouses;
@property(nonatomic, strong)     NSXMLParser *xmlParser;

-(void) initParsing;
@end

wsClass.m

#import "wsClass.h"

@implementation ProjListClass
@synthesize PROJECT_ID, SHORT_DESCR, LOCATION;
@end

@implementation wsClass

@synthesize arrHouses, xmlParser;

-(void) initParsing{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    soapResults = [[NSMutableString alloc] init];
    arrHouses=[[NSMutableArray alloc]init];

    NSString *soapMessage = [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"
                         "<Projects_List xmlns=\"http://tempuri.org/\"/>\n"
                         "</soap:Body>\n"
                         "</soap:Envelope>\n"];
   NSURL *url = [NSURL URLWithString:@"http://192.168.0.10:8056/WServices.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/Projects_List" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( conn )
{
    webData = [NSMutableData data];
}
else
{
    NSLog(@"theConnection is NULL");
}   
}
....
....
....
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    if (xmlParser)
  {
    xmlParser=nil;
  }
  xmlParser = [[NSXMLParser alloc] initWithData: webData];
  [xmlParser setDelegate: self];
  [xmlParser setShouldResolveExternalEntities:YES];
  [xmlParser parse];
 }

 ....
 ....
 ....
 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
.....
.....
.....
 else if([elementName isEqualToString:@"ProjectsListDetails"])
{
    [arrHouses addObject:proj_obj];
    proj_obj=nil;
}

soapResults = nil;
soapResults = [[NSMutableString alloc]init];
}

2 个答案:

答案 0 :(得分:1)

见下文步骤会给你一些想法

1)在您的NSOBject类内部说ParsingModel类,创建协议在解析类中进行解析。给出你想要的名字。

   @protocol  WebServiceResponseDelegate <NSObject>

   @optional
  - (void)didRecieveResponseData:(NSMutableArray*)array;

  @end

像这样将此协议实现到解析类    @protocol WebServiceResponseDelegate;

在解析类中创建属性,以便可以将Delegate设置为确认类..

      @property   (nonatomic, assign) id<WebServiceResponseDelegate>delegate;

并且只需要将解析数据发送到确认类,即从您创建解析对象的位置

   //say didRecieveResponse this is the method called when you completed parsing of data

 // - (void)didRecieveResponse
  // {   
  //  if([delegate respondsToSelector:@selector(didRecieveResponseData)])
 //    {
  //    //pass populated dataSource which would have the Parsed Data.

  //    [delegate didRecieveResponseData:parsedData];
 //    //  parsedData is the NUMtableArray or any other DataSource.

  //}

编辑:以下是成功完成配对后调用的delegate方法

  - (void)parserDidEndDocument:(NSXMLParser *)parser{
    //pass populated dataSource which would have the Parsed Data.  

    //before check whether that delegate method confirmed by the class.

    if([delegate respondsToSelector:@selector(didRecieveResponseData)])
     {
       [delegate didRecieveResponseData:parsedData];
     }

   //  parsedData is the NUMtableArray or any other DataSource.

   }
  // called when the parser has completed parsing. If this is encountered, the parse was successful.you need to call

2)正如您所说的那样UIViewControllerDataViewController确认类内部  定义该委托方法  在您的DataViewController.h中采用此协议DataViewController<WebServiceResponse>

从创建解析模型类的位置设置Delegate 像这样

  ParsingModel * paring = [ParsingModel alloc]init];
  [paring setWebServiceResponseDelegate:self];

 //definition of callback delegate method
 - (void)didRecieveResponseData:(NSMutableArray*)array
 {
 //here you would have the Parsed Data now you can use it as you want

 }

我希望你能清楚一切。

答案 1 :(得分:0)

您可以在NSObject类中创建一个协议,在所有解析操作完成后触发它,并将您的视图控制器设置为委托。