【发布时间】:2016-06-07 11:16:30
【问题描述】:
我的 get 请求仅在命令行 NSLog 中有效。 我需要在 Label 中显示数据,但它不起作用。
-(void)getRequest{
NSURLSessionConfiguration *getConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *getSession = [NSURLSession sessionWithConfiguration: getConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURL * getUrl = [NSURL URLWithString:@"http://localhost:3000/get"];
NSURLSessionDataTask * getDataTask = [getSession dataTaskWithURL:getUrl completionHandler:^(NSData *getData, NSURLResponse *getResponse, NSError *getError) {
if(getError == nil){
NSString * getString = [[NSString alloc] initWithData: getData encoding: NSUTF8StringEncoding];
[self.label setText:getString];// doesn't work!
NSLog(@"Data = %@",getString);}// it works!!
MainViewController*l=[[MainViewController alloc]init];
[l getRequest];
}
];
[getDataTask resume];
}
【问题讨论】:
-
你能检查你的标签是否为 nil 吗?通过在块中放置
NSLog(@"Data = %@", self.label);
标签: ios objective-c uilabel get-request