1
修改的UILabel我有以下定時器:iPhone/Objective-C的:如何從計時器
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數據設置三個UILabels。
我遇到的麻煩是UILabels沒有得到他們的文本集!儘管我可以在調試器中清楚地看到NSLog(@"xxx:%@",obj);
。 UILabel在.xib中正確連接。
任何幫助非常感謝。
UI元素只能從主循環更新。你確定你正在UI更新代碼的主循環上運行嗎? – 2012-07-23 08:43:41
嗯。我不知道。如何檢查這個?非常感謝, – Eamorr 2012-07-23 08:44:26
'ASIHTTPRequest'總是在主線程上回調。 – Lvsti 2012-07-23 08:48:16