2013-05-03 63 views
-1

我不知道爲什麼這很困難,但我無法得到它的工作。這是我的簡單問題:iOS:從NSObject類中的soap webservice傳遞解析值到UIViewController

我有一個UIViewController調用配對類類,執行解析工作。

我只需要將解析的數據值返回到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]; 
} 
+0

Just Simple使用函數返回該類的值:-) – 2013-05-03 11:52:27

回答

1

請參閱下列步驟會給你一些想法

1)裏面的你NSObject類說ParsingModel類裏面,你在做解析語法分析類中創建協議。給你想要的名字。

@protocol WebServiceResponseDelegate <NSObject> 

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

    @end 

實現此協議的解析類這樣 @protocol WebServiceResponseDelegate;

裏面的解析類創建屬性,這樣可以設置從確認類Delegate ..

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

和剛需將解析的數據發送給確認類i。從創建解析對象

//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. 

    //} 

編輯電子郵件:請參見下面是所謂的,當你完成了配對成功

- (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)照你說裏面的確認類的delegate方法UIViewControllerDataViewController define代理方法 在你的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 

} 

我希望一切你清楚。

+0

感謝Kamarshad,我編輯了我的線程並粘貼了我的代碼,可以請看看它並告訴我在代碼中要更改的位置...關心 – 2013-05-03 13:22:16

+0

這是什麼....?.... – Kamarshad 2013-05-03 13:24:20

+0

它是我正在使用的當前代碼,告訴我如何應用該協議。 – 2013-05-03 13:47:13

0

您可以在NSObject類創建一個協議,觸發它當你所有的解析操作已經完成,並設置您的視圖控制器作爲代表。

相關問題