我正在研究一个非常简单的应用程序,仅用于学习。这将celcius值转换为华氏值。它有一个带有两个UITextFields和一个按钮的视图。在第一个uitextfield中输入celcius值并单击按钮后的一个函数参数为“ - (void)requestCelciusandresponseFrnht :( NSString *)textfieldstring ”被调用。此方法在我的NSOBject类中,名为“ WebServiceCall ”。发送webservice,解析xml并将结果填充到名为“ soapResults ”的nsstring变量中。以下是我的代码结构:
我有一个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;
- (void)requestCelciusandresponseFrnht:(NSString *)textfieldstring;
实现代码以:
开头#import "WebServiceCall.h"
@implementation WebServiceCall @synthesize soapResults,test;
-(void)requestCelciusandresponseFrnht:(NSString *)textfieldstring{
//这里的代码用于肥皂消息,NSMUtableRequest,http heade,body&amp; NSURLConnection的 }
编辑:
- (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**
}
编辑:现在根据您的建议更改我的app委托类: 头文件.h: -
@class WebServicesViewController;
@interface WebServicesAppDelegate:NSObject { NSMutableString * results;
}
@property(nonatomic,retain)IBOutlet UIWindow * window;
@property(nonatomic,retain)IBOutlet WebServicesViewController * viewController; @property(非原子,保留)NSMutableString * results;
@end 实现文件.m as:
#import "WebServicesAppDelegate.h"
#import "WebServicesViewController.h"
@implementation WebServicesAppDelegate
@synthesize window = _window; @synthesize结果;
现在我正在尝试访问我的视图控制器中的soapResults变量这样的事情:
-(IBAction) buttonClicked:(id) sender
{NSLog(@“按钮点击,celcius值作为参数发送”);
clsvarWebServiceCall=[[WebServiceCall alloc]init];
[clsvarWebServiceCall requestandresponsewithfrnhtReq:txtcels.text];
WebServicesAppDelegate *appDelegate=(WebServicesAppDelegate *)[[UIApplication sharedApplication]delegate];
NSLog(@"appDelegate.results of viewController is %@",appDelegate.results);
这里我将soapResults值视为空
编辑:我的整个nslog / trace:`2011-09-07 15:03:22.948 WebServices [1717:207]视图控制器已加载
2011-09-07 15:03:41.266点击了Web服务[1717:207]按钮并将celcius值作为参数发送
2011-09-07 15:03:41.291 WebServices [1717:207] viewConle的appDelegate.results是(null)
2011-09-07 15:03:41.299 WebServices [1717:207] viewController的soapResults是(null)
2011-09-07 15:03:42.091 WebServices [1717:207]在nsobject类中调用的didreceiveresponse方法 2011-09-07 15:03:42.098 WebServices [1717:207]在nsobject类中调用didReceiveData方法
2011-09-07 15:03:42.103 Web服务[1717:207]在nsobject类中调用的connectionDidFinishLoading方法
2011-09-07 15:03:42.112 Web服务[1717:207]在nsobject类中调用的didStartElement方法
2011-09-07 15:03:42.117 WebServices [1717:207]在nsobject类中调用didStartElement方法
2011-09-07 15:03:42.122 WebServices [1717:207]在nsobject类中调用didStartElement方法
2011-09-07 15:03:42.127 Web服务[1717:207]在nsobject类中调用didStartElement方法
2011-09-07 15:03:42.133 WebServices [1717:207]在nsobject类中调用的foundCharacters方法
2011-09-07 15:03:42.139 Web服务[1717:207]在nsobject类中调用的didEndElement方法
2011-09-07 15:03:42.146 WebServices [1717:207] NSObject类中soapResult的值为:122
2011-09-07 15:03:42.159 WebServices [1717:207] NSObject类的appDelegate.results值是:122
2011-09-07 15:03:42.175 Web服务[1717:207]在nsobject类中调用的didEndElement方法
2011-09-07 15:03:42.179 WebServices [1717:207] NSObject类中soapResult的值如下:122
2011-09-07 15:03:42.184 WebServices [1717:207] NSObject类的appDelegate.results值是:122
2011-09-07 15:03:42.190 Web服务[1717:207]在nsobject类中调用的didEndElement方法
2011-09-07 15:03:42.196 WebServices [1717:207] NSObject类中soapResult的值为:122
2011-09-07 15:03:42.200 WebServices [1717:207] NSObject类的appDelegate.results值是:122
2011-09-07 15:03:42.204 Web服务[1717:207]在nsobject类中调用的didEndElement方法
2011-09-07 15:03:42.208 WebServices [1717:207] NSObject类中soapResult的值如下:122
2011-09-07 15:03:42.329 WebServices [1717:207] NSObject类的appDelegate.results值是:122 如果你看看nslog的soapREsults orappDelegate。首先调用视图控制器的soapResults,然后我知道为什么它为null并且soap结果的结果将在稍后出现,它无法在视图中更新soap结果值控制器? 有什么建议吗?
答案 0 :(得分:0)
您可以做的一件事是在appDelegate中声明一个全局字符串 NSMutableString * results;
并设置属性并合成它。之后当你在WebServiceCall中得到结果时,将结果设置为在appDelegate中声明的字符串
YourAppDelegateName * appDelegate =(YourAppDelegateName *)[[UIapplication sharedApplication] delegate]; appDelegate.results = [[NSString alloc] init]; appDelegate.results = soapResults;
并且在执行此操作之后,您可以在application.so之后的任何类上访问它 [clsvarWebServiceCall requestandresponsewithfrnhtReq:txtcels.text]; // 将celcius值作为参数发送到名为WebServiceCall的NSObject类的方法 写这个
YourAppDelegateName * appDelegate =(YourAppDelegateName *)[[UIapplication sharedApplication] delegate];
NSLog(@“结果为%@”,appDelegate.results);
或其他方法是当你能够获得soapResults 就在写完之后 [soapResults retain];
或者你可以使用singleton class.check这个文件 http://www.galloway.me.uk/tutorials/singleton-classes/
答案 1 :(得分:0)
尝试通过属性分配结果字符串并像这样访问它
self.soapResults = @"someValue"; //We are setting via property where it will be retained
并从您的视图控制器访问它。
答案 2 :(得分: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后控件返回viewcontroller时尝试从viewcontroller访问。