0


0

ツイートする前にCLLocationManagerが完了するのを待ちます

Latitude.textとlongtitude.textが入力されるのを待ってからツイートを送信します。このコードは正常に機能しますが、locationManagerにツイートする部分を入れたくないのは、ツイート。 これを行わずにツイートを送信する前に、txtが入力されていることを確認するにはどうすればよいですか?

- (IBAction)update {
    latitude.text [email protected]"";
    longitude.text [email protected]"";
    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:)];
[email protected]"";
 }

1 Answer


2


CLLocationManagerDelegateを実装するクラスにリスナーを登録できます。 例えば、

@interface GPS : NSObject  {

  CLLocationManager *locationManager;

  NSMutableArray *listeners;
}

- (void) addListener:(id)listener;
@end

@implementation GPS
- (IBAction)update {
  latitude.text [email protected]"";
  longitude.text [email protected]"";
  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 listener in listeners )
  {
    @try
    {
      [listener onLocationChangeForLatitude:latitude.text longitude:longitude];
    }
    @catch (NSException *e)
    {
      NSLog(@"Unhandled exception in onLocationChange");
    }
  }
  [pool release];
}

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

GPSオブジェクトに自分自身を登録するリスナーのセットを持つことができます。

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

これで、TweetGPSListenerを使用できます

@interface TweetGPSListener : NSObject  {
}
@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:)];
  [email protected]"";
}
@end

そのため、ツイートしたくない場合は、リスナーを登録しないか、リスナーを削除するか、ツイートに適切なタイミングを知るためのロジックをTweetListenerに追加します。