2013-11-20 30 views
1

我試圖從服務器發送和返回數據。我確信數據是可用的,因爲我可以在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]; 
} 
+0

是連接到的.xib文本字段出路何在? –

+0

如果你的文本視圖沒有顯示「測試」,爲什麼你發佈你的代碼服務器檢索? – Mundi

+0

是的,我把它連接到File'Owner – Stefano

回答

0

幾件事情:

@property (assign, nonatomic) IBOutlet UITextView *output; 

應該是strong性質類似這個:

@property (strong, nonatomic) IBOutlet UITextView *output; 

此外,請務必使用屬性訪問器這樣的:

​​

,並確保您File OwnerResultsViewController,它連接在的.xib

+0

謝謝Ralfonso,我做了更改,我也檢查了連接。但它不起作用。文件所有者它實際上是一類ResultsViewController,但不確定它是否連接到.xib。我要去搜索如何做到這一點。 – Stefano

+0

通過「確保它連接到.xib」,我的意思是確保你的IBOutlets連接到它們在Interface Builder中關聯的UIView組件。 – Ralfonso

+0

在這一行設置一個斷點:'self.value1TextField.text = @「test」;'驗證'value1TextField'不是零 – Ralfonso