在发推之前等待CLLocationManager完成

时间:2010-03-18 19:17:56

标签: objective-c iphone twitter

我想在发送推文之前等待latitude.text和longtitude.text填写,这段代码工作正常,但我宁愿不把推文部分放在locationManager中,因为我还想有时更新当前位置没有发送推文。如何在发送推文之前确保填写txt而不执行此操作?

- (IBAction)update {
    latitude.text =@"";
    longitude.text =@"";
    locmanager = [[CLLocationManager alloc] init]; 
    [locmanager setDelegate:self]; 
    [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locmanager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    CLLocationCoordinate2D location = [newLocation coordinate];
    latitude.text =   [NSString stringWithFormat: @"%f", location.latitude];
    longitude.text  = [NSString stringWithFormat: @"%f", location.longitude];

    TwitterRequest * t = [[TwitterRequest alloc] init];
    t.username = @"****";
    t.password = @"****";
    [twitterMessageText resignFirstResponder];
    loadingActionSheet = [[UIActionSheet alloc] initWithTitle:@"Posting To Twitter..." delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [loadingActionSheet showInView:self.view];
    [t statuses_update:twitterMessageText.text andLat:latitude.text andLong:longitude.text delegate:self requestSelector:@selector(status_updateCallback:)];
twitterMessageText.text=@"";
 }

1 个答案:

答案 0 :(得分:2)

您可以在类中注册实现CLLocationManagerDelegate的侦听器。例如,

@interface GPS : NSObject <CLLocationManagerDelegate> {

  CLLocationManager *locationManager;

  NSMutableArray *listeners;
}

- (void) addListener:(id<GPSListener>)listener;
@end

@implementation GPS
- (IBAction)update {
  latitude.text =@"";
  longitude.text =@"";
  locmanager = [[CLLocationManager alloc] init]; 
  [locmanager setDelegate:self]; 
  [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
  [locmanager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{ 
  CLLocationCoordinate2D location = [newLocation coordinate];
  latitude.text =   [NSString stringWithFormat: @"%f", location.latitude];
  longitude.text  = [NSString stringWithFormat: @"%f", location.longitude];

  // Inform the listeners.
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  for ( id<GPSListener> listener in listeners )
  {
    @try
    {
      [listener onLocationChangeForLatitude:latitude.text longitude:longitude];
    }
    @catch (NSException *e)
    {
      NSLog(@"Unhandled exception in onLocationChange");
    }
  }
  [pool release];
}

- (void) addListener:(id<GPSListener>)listener
{  
  [listeners addObject:listener];
}
@end 

您可以拥有一组用GPS对象注册的听众。

@protocol GPSListener {
- (void) onLocationChangeForLatitude:(NSString *)latitude longitude:(NSString *)longitude;
}

所以现在你可以拥有一个TweetGPSListener

@interface TweetGPSListener : NSObject <GPSListener> {
}
@end

@implementation TweetGPSListener
- (void) onLocationChangeForLatitude:(NSString *)latitude longitude:(NSString *)longitude {
  TwitterRequest * t = [[TwitterRequest alloc] init];
  t.username = @"****";
  t.password = @"****";
  [twitterMessageText resignFirstResponder];
  loadingActionSheet = [[UIActionSheet alloc] initWithTitle:@"Posting To Twitter..."     delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
  [loadingActionSheet showInView:self.view];
  [t statuses_update:twitterMessageText.text andLat:latitude andLong:longitude   delegate:self requestSelector:@selector(status_updateCallback:)];
  twitterMessageText.text=@"";  
}  
@end

因此,对于您不想发送推文的情况,请不要注册侦听器,删除侦听器或向TweetListener添加一些逻辑以了解何时适合发推文。