2011-09-06 62 views
0

我在一个非常简单的应用程序只为learning.This工作摄氏转换成价值华氏value.It具有两个UITextFields和button.After的单一视图在第一个uitextfield中输入一个celcius的值,然后点击一个参数为“ - (void)requestCelciusandresponseFrnht :(NSString *)textfieldstring”的函数。这个方法在我的NSOBject类中名为“WebServiceCall”。 Web服务发送,解析XML并填充结果为“soapResults”。下面是我的代码结构的NSString变量:accesing一类nsmutable串在另一个班级成绩空目标C

我有变量NSObject类为“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; 

- (无效)requestCelciusandresponseFrnht:(的NSString *)textfieldstring;

实现代码为开始:

#import "WebServiceCall.h" 

@implementation WebServiceCall @synthesize soapResults,测试;

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

//这里的代码也适用于SOAP消息,NSMUtableRequest,HTTP heade,身体& NSURLConnection的 }

编辑:

- (空)解析器:(*的NSXMLParser)分析器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**  

}

编辑:现在根据我的应用程序委托类的建议变化: 头文件的.h: -

@class WebServicesViewController; 

@interface WebServicesAppDelegate:NSObject的{ 的NSMutableString *的结果;

}

@属性(非原子,保留)IBOutlet中的UIWindow *窗口;

@属性(非原子,保留)IBOutlet中WebServicesViewController *的viewController; @属性(非原子,保留)的NSMutableString *的结果;

@end 实现文件。米为:

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

@implementation WebServicesAppDelegate

@synthesize窗口= _window; @synthesize结果;

现在我试图访问soapResults变量在我的视图控制器一些事情是这样的:

-(IBAction) buttonClicked:(id) sender 

{的NSLog(@“按钮点击,摄氏值发送作为参数”);

clsvarWebServiceCall=[[WebServiceCall alloc]init]; 

[clsvarWebServiceCall requestandresponsewithfrnhtReq:txtcels.text]; 

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

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

这里我得到soapResults一个空值

编辑:我的整个的NSLog /跟踪:`2011-09-07 15:03:22.948的WebServices [1717:207]视图控制器加载

2011-09-07 15:03:41.266 Web服务[1717:207]按钮点击和摄氏值发送作为参数

2011-09-07 15:03:41.291 Web服务[1717:207] appDelegate.results viewController是(null)

2011-09-07 15:03:41.299 Web服务[1717:207]的viewController的soapResults是(空)

2011-09-07 15:03:42.091 Web服务[1717:207] didreceiveresponse方法称为在NSObject类 2011-09-07 15:03:42.098 Web服务[1717:207] didReceiveData方法调用NSObject类

2011-09-07 15:03:42.103 Web服务[1717:207] connectionDidFinishLoading调用方法nsobject类

2011-09-07 15:03:42.112 WebServices [1717:207] didStartElement方法在nsobject类中调用

2011-09-07 15:03:42.117 Web服务[1717:207] didStartElement方法调用NSObject类

2011-09-07 15:03:42.122 Web服务[1717:207] didStartElement方法称为NSObject类

2011-09-07 15:03:42.127 Web服务[1717:207] didStartElement方法调用NSObject类

2011-09-07 15:03:42.133 Web服务[1717:207] foundCharacters方法叫做nsobject类

2011-09-07 15:03:42.139 WebServices [1717:20 7]称为NSObject类didEndElement方法

2011-09-07 15:03:42.146 Web服务[1717:207] soapResult在NSObject类的值如下:122

2011-09-07 15: 03:42.159 Web服务[1717:207] NSObject类的appDelegate.results值为:122

2011-09-07 15:03:42.175 Web服务[1717:207] didEndElement方法调用NSObject类

2011 -09-07 15:03:42。Web服务179 [1717:207] soapResult在NSObject类的值如下:122

2011-09-07 15:03:42.184 Web服务[1717:207] NSObject类的appDelegate.results值为:122

2011-09-07 15:03:42.190 Web服务[1717:207] didEndElement方法调用NSObject类

2011-09-07 15:03:42.196 Web服务[1717:207] soapResult在NSObject的值class为:122

2011-09-07 15:03:42.200 WebServices [1717:207] appDelegate.results NSObject类的值为:122

2011-09-07 15:03:42.204 Web服务[1717:207] didEndElement方法调用NSObject类

2011-09-07 15:03:42.208 Web服务[1717:207] soapResult在值NSObject类是:122

2011-09-07 15:03:42.329 WebServices [1717:207]如果您查看nslog中的soapREsults orappDelegate,则NSObject类的appDelegate.results值为:122` 。 肥皂结果的视图控制器被称为第一,我thik多数民众党多数民众权局这就是为什么它为空,从肥皂结果的结果是后来,它无法更新视图控制器中的肥皂结果值? 有什么建议吗?

回答

0
` 

`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; 

,并尝试从视图 - 控制时,控制返回到越来越soapResults后视图 - 控制访问。

+0

,这里不要忘记释放viewcontroller对象字符串对象,否则会产生内存泄漏。 – Bhoomi

0

你可以做的一件事是在appDelegate中声明一个全局字符串 NSMutableString * results;

,并设置属性和合成it.after,当你得到的结果WebServiceCall设置结果中的appDelegate

YourAppDelegateName *的appDelegate =(YourAppDelegateName *)[[UIApplication的sharedApplication]委托]声明的字符串; appDelegate.results = [[NSString alloc] init]; appDelegate.results = soapResults;

,做这之后您可以能够访问这个在application.so任何课后 [clsvarWebServiceCall requestandresponsewithfrnhtReq:txtcels.text]; // 发送摄氏值作为参数传递给NSObject类的命名WebServiceCall 方法写这个

YourAppDelegateName * appDelegate =(YourAppDelegateName *)[[UIapplication sharedApplication] delegate]; NSLog(@“result is%@”,appDelegate.results);

或其他方法是当你能够得到肥皂结果 刚写完 [soapResults retain];

,或者您可以使用单class.check本文 http://www.galloway.me.uk/tutorials/singleton-classes/

+0

您好我有folow中的appdelegate的方法,但仍然得到价值null.I已观察到一两件事很奇怪,但我无法理解它:点击按钮,并通过论证在另一个之后(nsobjectclass用于Web服务调用)类方法的NSLog第一得到日志uiviewcontroll.Taking您的代码:YourAppDelegateName *的appDelegate =(YourAppDelegateName *)[[UIApplication的sharedApplication]委托]; NSLog(@“result is%@”,appDelegate.results);.它GIVS空,然后编译/ NSLog的打印值NSObject的class.See的soapResult我的编辑 – Alok

+0

您有没有代码,而不是YourAppDelegateName.can写你的应用程序委托名称,你告诉我到底你webservicecall类添加什么代码? – Bhoomi

+0

请参阅上述编辑我试图让尽可能多的相关信息,我认为这个问题是在调用webservic类似:1.button点击和发送数据从视图控制器NSObject类。 2.现在,nsobject类变量soapResults保存来自解析xml.3的值。现在,它将appdelegate文件中的结果值更新为:appDelegate.results = soapResults。 4.now访问该从视图中controller.BUT在我的情况下视图控制器的appDelegate.results appdelegate.results被称为第一,所以它是空,然后被填充NSObject中的soapResults值.. – Alok

0

尝试通过属性和访问它像这样

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

,并从您的视图控制器访问其分配结果字符串。

相关问题