0
votes

I am working on a very simple app just for learning.This convert celcius value into fahrenheit value.It has a single view with two UITextFields and a button.After entering a celcius value in 1st uitextfield and clicking on the button a function with argument as "-(void)requestCelciusandresponseFrnht:(NSString *)textfieldstring" is called.This method is in my NSOBject class named "WebServiceCall". webservice is sent, xml is parsed and the result is populated in a nsstring variable named "soapResults".Below is the structure of my code:

i am having a NSObject class with variable as "soapresults":

@interface WebServiceCall : NSObject <UITextFieldDelegate,NSXMLParserDelegate> {
NSString *msgLength;    
//web service access
NSMutableData *webData;//contains modifiable data in form of bytes
NSMutableString *soapResults;//To hold the value of fahrenheit from parsed xml resulting thrugh webservice
BOOL elementFound;
ends
NSURLConnection *conn;
NSXMLParser *xmlParser;       

}

@property (nonatomic,retain) NSMutableString *soapResults;

-(void)requestCelciusandresponseFrnht:(NSString *)textfieldstring;

the implementation code starts as:

#import "WebServiceCall.h"

@implementation WebServiceCall @synthesize soapResults,test;

-(void)requestCelciusandresponseFrnht:(NSString *)textfieldstring{

//here the code goes for soap message,NSMUtableRequest,http heade,body & NSURLconnection }

EDIT:

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

NSLog(@"the value of soap result is as : %@",soapResults);//**i am able to get fahrenheit value here**

WebServicesAppDelegate *appDelegate=(WebServicesAppDelegate *)[[UIApplication sharedApplication]delegate];

appDelegate.results=[[NSString alloc]init];

appDelegate.results=soapResults;

NSLog(@"appDelegate.results value of NSObject class  is :%@",appDelegate.results);

//**i am able to set fahrenheit value here in appDelegate.results**    

}

Edit:Now according to your suggestion changes in my app delegate class as: The header file .h:-

@class WebServicesViewController;

@interface WebServicesAppDelegate : NSObject { NSMutableString *results;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet WebServicesViewController *viewController; @property (nonatomic, retain) NSMutableString *results;

@end the implementation file .m as:

#import "WebServicesAppDelegate.h"
#import "WebServicesViewController.h"

@implementation WebServicesAppDelegate

@synthesize window=_window; @synthesize results;

now i am trying to access the soapResults variable in my view controller some thing like this:

-(IBAction) buttonClicked:(id) sender

{ NSLog(@"button clicked and celcius value sent as argument");

clsvarWebServiceCall=[[WebServiceCall alloc]init];

[clsvarWebServiceCall requestandresponsewithfrnhtReq:txtcels.text];

WebServicesAppDelegate *appDelegate=(WebServicesAppDelegate *)[[UIApplication sharedApplication]delegate];

NSLog(@"appDelegate.results of viewController is %@",appDelegate.results); 

here i am getting soapResults Value as null

EDIT:my whole nslog/trace :`2011-09-07 15:03:22.948 WebServices[1717:207] view controller loaded

2011-09-07 15:03:41.266 WebServices[1717:207] button clicked and celcius value sent as argument

2011-09-07 15:03:41.291 WebServices[1717:207] appDelegate.results of viewController is (null)

2011-09-07 15:03:41.299 WebServices[1717:207] soapResults of viewController is (null)

2011-09-07 15:03:42.091 WebServices[1717:207] didreceiveresponse method called in nsobject class 2011-09-07 15:03:42.098 WebServices[1717:207] didReceiveData method called in nsobject class

2011-09-07 15:03:42.103 WebServices[1717:207] connectionDidFinishLoading method called in nsobject class

2011-09-07 15:03:42.112 WebServices[1717:207] didStartElement method called in nsobject class

2011-09-07 15:03:42.117 WebServices[1717:207] didStartElement method called in nsobject class

2011-09-07 15:03:42.122 WebServices[1717:207] didStartElement method called in nsobject class

2011-09-07 15:03:42.127 WebServices[1717:207] didStartElement method called in nsobject class

2011-09-07 15:03:42.133 WebServices[1717:207] foundCharacters method called in nsobject class

2011-09-07 15:03:42.139 WebServices[1717:207] didEndElement method called in nsobject class

2011-09-07 15:03:42.146 WebServices[1717:207] the value of soapResult in NSObject class is as : 122

2011-09-07 15:03:42.159 WebServices[1717:207] appDelegate.results value of NSObject class is :122

2011-09-07 15:03:42.175 WebServices[1717:207] didEndElement method called in nsobject class

2011-09-07 15:03:42.179 WebServices[1717:207] the value of soapResult in NSObject class is as : 122

2011-09-07 15:03:42.184 WebServices[1717:207] appDelegate.results value of NSObject class is :122

2011-09-07 15:03:42.190 WebServices[1717:207] didEndElement method called in nsobject class

2011-09-07 15:03:42.196 WebServices[1717:207] the value of soapResult in NSObject class is as : 122

2011-09-07 15:03:42.200 WebServices[1717:207] appDelegate.results value of NSObject class is :122

2011-09-07 15:03:42.204 WebServices[1717:207] didEndElement method called in nsobject class

2011-09-07 15:03:42.208 WebServices[1717:207] the value of soapResult in NSObject class is as : 122

2011-09-07 15:03:42.329 WebServices[1717:207] appDelegate.results value of NSObject class is :122` if you look at the nslog the soapREsults orappDelegate.soapResults of view controller is called first and i thik thats why it is null and the result from the soap result is coming later and it is unable to update the soap result value in view controller? any suggestion?

3

3 Answers

0
votes

Try assigning the result string via property and access it like this

self.soapResults = @"someValue"; //We are setting via property where it will be retained

and access it from your view controller.

0
votes
`

`Another thing you can do is Declare object of viewcontroller in NSObject class.
NSobject.h


YourViewControllerName *viewController;

declare the properties and synthesize it in .m file

Declare one string in viewcontroller file

NSMutableString *getResult;


and when you get the result in NSObject class
after that do this

viewController=[[YourViewControllerName alloc]init];
viewController.getResult=[[NSMutableString alloc]init];
viewControoler.getResult=soapResults;

and try to access from viewcontroller when control returns to viewcontroller after getting soapResults.

0
votes

One thing you can do is declare one global string in appDelegate NSMutableString *results;

and set properties and synthesize it.after that when you get the result in WebServiceCall set that result to string declared in appDelegate

YourAppDelegateName *appDelegate=(YourAppDelegateName *)[[UIapplication sharedApplication]delegate]; appDelegate.results=[[NSString alloc]init]; appDelegate.results=soapResults;

and after doing this you can able to access this on any class in application.so after [clsvarWebServiceCall requestandresponsewithfrnhtReq:txtcels.text];//sending celcius value as argument to the method of NSObject class named WebServiceCall write this

YourAppDelegateName *appDelegate=(YourAppDelegateName *)[[UIapplication sharedApplication]delegate];

NSLog(@"result is %@",appDelegate.results);

or other approach is when you are able to get soapResults just after that write [soapResults retain];

or you can use singleton class.check this document http://www.galloway.me.uk/tutorials/singleton-classes/