0

我有一种方法给我带来了一些问题。我正在尝试一个接一个地执行两种方法,但是第二种方法在第一种方法完成之前一直在启动。我相当肯定这与第一种方法中有一个块有关,但由于我不太了解它们,我无法修复它,甚至无法在此处使用其他答案。任何帮助或建议将不胜感激!

顶级方法:

- (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];
    }
}

当我在一种方法中拥有所有代码时,一切正常,但要继续我的项目,我需要能够分离出这些操作。

谢谢你提供的所有帮助!

4

1 回答 1

0

你必须这样称呼它:

[self setLocationWithCompletionHandler:^(NSString *message) {
    [self sendInAppSMS:message];
}];

将您的方法转换为:

- (void)setLocationCompletionHandler:(void (^)(NSString *message))completionHandler{
    ....
    dispatch_async(dispatch_get_main_queue(), ^{
        if (completionHandler) completionHandler(globalLocation);
    });
}
于 2013-03-14T13:07:45.580 回答