我有一个UIView
,其中我有UILabel
,当我按下按钮时我从服务器接收数据,我想根据新数据更改标签中的文字,我已设法做到这一点,但需要很长时间(约一分钟)来更改文本。
有谁知道为什么?因为我在更改后使用NSLog
来显示lbl.text
并显示新数据但在屏幕上需要时间才能更改..
NSLog(@"Sending Data to Server");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(@"post: %@",post);
NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *urlstring = [NSString stringWithFormat:JSON_URL];
[request setURL:[NSURL URLWithString:urlstring]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
dispatch_async(dispatch_get_main_queue()
NSError *error;
NSURLResponse *response;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
_jsonResults = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];
Label.text = [NSString stringWithFormat:@"you have: %@",[_jsonResults objectForKey:@"num"]];
我编辑了我的答案,现在它工作正常,我只在后台线程上更新我的标签,所以我添加了
dispatch_async(dispatch_get_main_queue()
现在效果很好!
答案 0 :(得分:0)
我在后端做了这就是为什么花了这么多时间来改变, 我加上这个 dispatch_async(dispatch_get_main_queue()
现在它工作正常!