views:

136

answers:

1

I want to wait for latitude.text and longtitude.text to be filled in before sending a tweet, this code works fine, but I would rather not put the tweeting part in locationManager because I also want to sometimes update the current location without sending a tweet. How can I make sure the txt gets filled in before sending the tweet without doing this?

- (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=@"";
 }
A: 

You could register listeners in your class that implements the CLLocationManagerDelegate. For example,

@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 

The you can have a set of listeners that register themselves with your GPS object.

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

So now you can have a 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

So for cases that you do not want to tweet either do not register the listener, remove the listener or add some logic to the TweetListener to know when it is appropriate to tweet.

Randy Simon
That seems really complicated as in I have no clue what you are saying (I just started learning objective C 3 days ago)Can I just have another locationmanager functon?
It is really not that complicated. I don't know what you mean by "another locationmanager function". I thought that you wanted to move your code out of the location manager so I was giving you a way to do that. Also, by using a listener other parts of your app can decide when to register or remove the listener. I suggest spending some time understanding some Objective-C concepts (like protocols) and that may help you come up with a solution.
Randy Simon