dispatch_get_global_queue
Web 呼び出しを非同期で実行する方法を学ぼうとしています。基本的に、ユーザーの場所に応じてユーザー インターフェイスを更新する必要があります。dispatch_get_global_queue
Web通話中にユーザーインターフェースが反応しなくなったので使い始めました。
今、私の問題は、buttonUpdate が呼び出されたときにボタンが正しく表示されないことです。ボタンの 1 つは黒、もう 1 つは緑です。ただし、現在はどちらもデフォルトの青で表示されます。また、ボタンの単語/タイトルは、いずれかをクリックするまで表示されません。理由を知っている人はいますか?ありがとうございました!
preButtonUpdate
buttonUpdate メソッドを非同期的に呼び出すこのメソッドを呼び出しました。
-(void)preButtonUpdate {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
[self buttonUpdate];
});
}
preButtonUpdate メソッドは、viewDidAppear および appReturnsActive で呼び出されます。
- (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];
}
}