繁体   English   中英

在发推文之前等待CLLocationManager完成

[英]wait for CLLocationManager to finish before tweeting

我想在发送推文之前等待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=@"";
 }

您可以在实现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添加一些逻辑以了解何时适当进行鸣叫。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM