2010-03-18 58 views
0

我想在发送推文之前等待latitude.text和longtitude.text填充,此代码正常工作,但我宁愿不将tweeting部分放在locationManager中,因为我也想要有时更新当前位置而不发送推文。我怎么才能确保在发送推文之前没有这样做txt被填充?等待CLLocationManager在发送推文之前完成

- (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]""; 
} 

回答

2

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

@interface GPS : NSObject <CLLocationManagerDelegate> { 

    CLLocationManager *locationManager; 

    NSMutableArray *listeners; 
} 

- (void) addListener:(id<GPSListener>)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<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:)]; 
    [email protected]""; 
} 
@end 

因此,对于你不想鸣叫或者不注册侦听器,删除监听或添加一些逻辑到TweetListener知道案件什么时候适合推特。

+0

这似乎很复杂,因为我不知道你在说什么(我刚刚开始学习目标C 3天前) 我可以只有另一个位置管理函数吗? – user295944 2010-03-18 21:21:20

+0

它确实不那么复杂。我不知道你的意思是“另一个位置管理功能”。我以为你想把你的代码从位置管理器中移出来,所以我给了你一个办法。另外,通过使用监听器,您应用的其他部分可以决定何时注册或移除监听器。我建议花一些时间理解一些Objective-C概念(如协议),这可能会帮助您提出解决方案。 – 2010-03-18 22:09:08

相关问题