我正在尝试学习使用dispatch_get_global_queue
异步执行网络调用。基本上,我需要根据用户的位置更新用户界面。我开始使用dispatch_get_global_queue
是因为在执行网络呼叫时用户界面没有响应。
现在,我的问题是调用 buttonUpdate 时按钮显示不正确。一个按钮应该是黑色的,另一个是绿色的。但是,现在它们都显示为默认的蓝色。此外,在我单击其中一个之前,不会出现按钮的单词/标题。有谁知道为什么?谢谢!
我preButtonUpdate
调用了这个方法,它异步调用 buttonUpdate 方法:
-(void)preButtonUpdate {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
[self buttonUpdate];
});
}
在 viewDidAppear 和 appReturnsActive 中调用 preButtonUpdate 方法:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self preButtonUpdate];
//other code
}
- (void)appReturnsActive {
[myTimer invalidate];
myTimer = nil;
//ONLY WANT TO PERFORM THE UPDATE IF VIEWING THE PROFILE VIEW CONTROLLER
UIViewController *currentVC = self.navigationController.visibleViewController;
NSString * name = NSStringFromClass([currentVC class]);
if ([name isEqualToString:@"ProfileViewController"]) {
[self preButtonUpdate];
}
}
然后,preButtonUpdate 调用 buttonUpdate 方法,该方法执行 Web 查询并更新用户界面:
-(void) buttonUpdate {
[locationManagerProfile stopUpdatingLocation];
NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLatitude];
NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLongitude];
NSString *placeLatitude = [[NSUserDefaults standardUserDefaults]
stringForKey:@"savedLatitude"];
NSString *placeLongitude = [[NSUserDefaults standardUserDefaults]
stringForKey:@"savedLongitude"];
NSString *distanceURL = [NSString stringWithFormat:@"http://www.website.com/page.php?
lat1=%@&lon1=%@&lat2=%@&lon2=%@",userLatitude, userLongitude, placeLatitude,
placeLongitude];
NSData *distanceURLResult = [NSData dataWithContentsOfURL:[NSURL
URLWithString:distanceURL]];
NSString *distanceInFeet = [[NSString alloc] initWithData:distanceURLResult
encoding:NSUTF8StringEncoding];
if ([distanceInFeet isEqualToString:@"1"]) {
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Button A"
style:UIBarButtonItemStyleBordered target:self action:@selector(actionA)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor
colorWithRed:44.0/255.0 green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIBarButtonItem *btnGoTwo = [[UIBarButtonItem alloc] initWithTitle:@"Button B"
style:UIBarButtonItemStyleBordered target:self action:@selector(actionB)];
self.navigationItem.rightBarButtonItem = btnGoTwo;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnGo,
btnGoTwo, nil];
}
if ([distanceInFeet isEqualToString:@"0"]) {
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Button C"
style:UIBarButtonItemStyleBordered target:self action:@selector(actionC)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor
colorWithRed:44.0/255.0 green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIBarButtonItem *btnGoTwo = [[UIBarButtonItem alloc] initWithTitle:@"Button B"
style:UIBarButtonItemStyleBordered target:self action:@selector(actionB)];
self.navigationItem.rightBarButtonItem = btnGoTwo;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnGo,
btnGoTwo, nil];
}
}