我有一种给我一些问题的方法。我试图一个接一个地执行两个方法,但第二个方法在第一个方法完成之前保持启动。我相当肯定它与第一种方法中有一个块的事实有关,但由于我不太了解它们,我无法修复它甚至在这里使用其他答案。任何帮助或建议都会非常感激!
顶级方法:
- (IBAction)SendTextTapped:(id)sender{
NSLog(@"Entered tapped method");
[self setLocation];
NSLog(@"Supposedly past setLocation");
[self sendInAppSMS:globalLocation];
}
第一个辅助方法:
- (void)setLocation{
CLLocation *location = locationManager.location;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *newerLocation =[[CLLocation alloc]initWithLatitude:location.coordinate.latitude
longitude:location.coordinate.longitude];
NSLog(@"%f",location.coordinate.longitude);
[geocoder reverseGeocodeLocation:newerLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Geocode failed with error: %@", error);
return;
}
//NSLog(@"Entered geocoder");
if (placemarks && placemarks.count > 0) {
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary =
placemark.addressDictionary;
NSLog(@"%@ ", addressDictionary);
NSString *address = [addressDictionary objectForKey:(NSString *)kABPersonAddressStreetKey];
globalLocation=[NSString stringWithFormat:@"pickup: %@, %@\n person: Joe Blow", address,placemark.subLocality];
NSLog(globalLocation);
dispatch_async(dispatch_get_main_queue(), ^{});
}
}];
}
第二个辅助方法:
-(void) sendInAppSMS:(NSString *)message
{
NSLog(@"Entered sendInAppSMS");
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = message;
controller.recipients = [NSArray arrayWithObjects:@"123456", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
当我在一个方法中拥有所有代码时,一切正常,但是为了继续我的项目,我需要能够将操作分开。
感谢您提供任何帮助!
答案 0 :(得分:0)
你必须这样称呼:
[self setLocationWithCompletionHandler:^(NSString *message) {
[self sendInAppSMS:message];
}];
将您的方法转换为:
- (void)setLocationCompletionHandler:(void (^)(NSString *message))completionHandler{
....
dispatch_async(dispatch_get_main_queue(), ^{
if (completionHandler) completionHandler(globalLocation);
});
}