我正在尝试从服务器发送和返回数据。我确信数据是可用的,因为我可以在ResultsViewController.m中使用NSLog在目标输出中打印出来,但是使用TextView我不能。
这是代码:
ResultsViewController.h
@interface ResultsViewController : UIViewController <ServiceConnectorDelegate, UITextViewDelegate>
{}
@property (assign, nonatomic) IBOutlet UITextView *output;
@property (retain, nonatomic) IBOutlet UITextView *value1TextView;
@property (retain, nonatomic) IBOutlet UITextField *value1TextField;
- (void)sendString:(NSString *)str;
@end
ResultsViewController.m
- (void)sendString:(NSString *)str
{
ServiceConnector *serviceConnector = [[ServiceConnector alloc] init];
serviceConnector.delegate = self;
[serviceConnector getTest: str];
}
- (void)requestReturnedData:(NSData *)data //called when data is returned
{
NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:data];
output.text = dictionary.JSONString; // set the textview to the raw string value of the data received
value1TextField.text = [NSString stringWithFormat:@"%d",[[dictionary objectForKey:@"value"] intValue]];
value1TextField.text = @"test";
NSLog(@"%@",dictionary);
}
ServiceConnector.m
@implementation ServiceConnector
{
NSData *receivedData;
}
- (void)getTest:(NSString *)str
{
NSString *storedURL = str;
NSString *urlstring = [NSString stringWithFormat:@"%@",storedURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlstring]];
[request setHTTPMethod:@"GET"];
[request addValue:@"getValues" forHTTPHeaderField:@"METHOD"]; //selects what task the server will perform
//initialize an NSURLConnection with the request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection){
NSLog(@"Connection Failed");
}
}
可能是TextView存在问题,因为它没有响应这么简单的代码:
value1TextField.text = @"test";
要将应用程序连接到服务器,我使用Service Connector的委托,但我不确定我是否以正确的方式实现了委托。特别是当我定义
时serviceConnector.delegate = self;
在另一个应用程序中,我使用Button实现委托,它可以工作:
- (IBAction)getDown:(id)sender
{
//perform get request
ServiceConnector *serviceConnector = [[ServiceConnector alloc] init];
serviceConnector.delegate = self;
[serviceConnector getTest];
}
答案 0 :(得分:0)
有几件事:
@property (assign, nonatomic) IBOutlet UITextView *output;
应该是strong
这样的属性:
@property (strong, nonatomic) IBOutlet UITextView *output;
另外,请确保使用以下属性访问器:
self.value1TextField.text = @"test";
并确保您的File Owner
为ResultsViewController
并且它已连接到.xib