我試圖從服務器發送和返回數據。我確信數據是可用的,因爲我可以在ResultsViewController.m中使用NSLog在目標輸出中打印它們,但使用TextView我不能。TextView不顯示消息,NSLog確實
這裏是代碼:
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");
}
}
有可能是一個proble米,TextView的,因爲它沒有這樣一個簡單的代碼來回應他們:
value1TextField.text = @"test";
要連接的應用我使用服務連接器的委託服務器,但我不知道,我已經實現了在委託正確的方式。尤其是當我定義
serviceConnector.delegate = self;
在另一個應用程序,我用一個按鈕實現委託和它的作品:
- (IBAction)getDown:(id)sender
{
//perform get request
ServiceConnector *serviceConnector = [[ServiceConnector alloc] init];
serviceConnector.delegate = self;
[serviceConnector getTest];
}
是連接到的.xib文本字段出路何在? –
如果你的文本視圖沒有顯示「測試」,爲什麼你發佈你的代碼服務器檢索? – Mundi
是的,我把它連接到File'Owner – Stefano