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 {
CLLocationCo开发者_如何学JAVAordinate2D 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=@"";
}
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.
精彩评论