2010-03-18 59 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

相關問題