2010-03-18 8 views
0

트윗을 보내기 전에 latitude.text 및 longtitude.text가 채워지기를 기다리고 싶습니다.이 코드는 잘 작동하지만 locationManager에 tweeting 부분을 넣지는 않을 것입니다. 트윗을 보내지 않고 현재 위치를 업데이트하려고합니다. 이렇게하지 않고 트윗을 보내기 전에 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 당신이 트윗하지 않으 중 하나에 리스너를 등록하지 않는, 리스너를 제거하거나 알 수 있도록 TweetListener 일부 로직을 추가 할 경우에 따라서

@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 

을 가질 수 있습니다 짹짹는 것이 적당 할 때.

+0

정말 복잡한 것처럼 보입니다. 당신이 말하는 것을 전혀 알지 못합니다. (방금 3 일 전에 객관적인 C를 배우기 시작했습니다.) 다른 위치 관리자 functon을 사용할 수 있습니까? – user295944

+0

정말 그렇게 복잡한 것은 아닙니다. "다른 위치 관리자 기능"이 무슨 뜻인지는 모르겠다. 위치 관리자에서 코드를 옮기고 싶다고 생각 했으므로 그렇게 할 수있는 방법을 제공했습니다. 또한 리스너를 사용하여 앱의 다른 부분에서 리스너를 등록하거나 제거 할시기를 결정할 수 있습니다. 몇 가지 Objective-C 개념 (프로토콜과 같은)을 이해하는 데 시간을 투자하고 솔루션을 생각해내는 데 도움이 될 것이라고 제안합니다. –

관련 문제