2013-11-20 33 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