1

我有以下计时器:

uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(uploadGPS_tick:) userInfo:nil repeats:YES];
[self uploadGPS_tick:nil];

这是回调:uploadGPS_tick():

-(void)uploadGPS_tick:(NSTimer*)timer{
    if(!lat || !lng){
        //do nothing
    }else{
        NSString *urlStr=[[NSString alloc] initWithFormat:@"http://www.example.com/ajax/updateCoords.php"];
        NSURL *url=[NSURL URLWithString:urlStr];

        __block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
        [request setPostValue:lat forKey:@"lat"];
        [request setPostValue:lng forKey:@"lng"];
        NSLog(@"EOH: %@",lat);
        [request setDelegate:self];
        [request setCompletionBlock:^{
            NSString *response=[request responseString];
            NSLog(@"%@",response);

            if([response isEqualToString:@"LO"]){
                [self.navigationItem setBackBarButtonItem:nil];
                DriverLogin *x= [[DriverLogin alloc] initWithNibName:nil bundle:nil];
                [[self navigationController]pushViewController:x animated:NO];
            }

            SBJsonParser *parser=[[SBJsonParser alloc]init];
            NSMutableDictionary *obj=[[NSMutableDictionary alloc]init ];
            obj=[[parser objectWithString:[request responseString] error:nil]retain];

            credits.text=[[obj objectForKey:@"credits"] stringValue];   //this won't show...
            creditsUsed.text=[[obj objectForKey:@"creditsUsed"] stringValue];   //this won't show...
            NSInteger timeLeftSecs=[[obj objectForKey:@"creditTimeLeft"] intValue];
            NSInteger timeLeftMins=(int)(timeLeftSecs/60);
            creditTimeLeft.text=[[NSString alloc]initWithFormat:@"%d",timeLeftMins];   //this won't show...

            NSLog(@"xxx:%@",obj);

        }];
        [request setFailedBlock:^{
            NSError *error =[request error];
            NSLog(@"%@",error);
            //do nothing
        }];
        [request startAsynchronous];
    }
}

如您所见,每五秒钟,服务器就会发送一个 JSON 对象。然后解析这个 JSON 对象,并根据这个 JSON 数据设置三个 UILabel。

我遇到的麻烦是 UILabel 没有得到他们的文本集!即使我可以NSLog(@"xxx:%@",obj);在调试器中清楚地看到。UILabel 在 .xib 中正确连接。

非常感谢任何帮助。

4

1 回答 1

2

您应该从主线程执行此操作。用以下代码替换标签文本分配:

dispatch_async(dispatch_get_main_queue(), ^{
    creditTimeLeft.text=[NSString stringWithFormat:@"%d",timeLeftMins];

});
于 2012-07-23T10:50:07.943 回答