2014-06-09 2 views
1

위치 관리자 개체에서 내 viewController로 알림을 보내려고합니다. 작동하지 않습니다. addObserver 메소드의 selector가 호출되지 않습니다.NSNotification postNotification 및 addObserver

위치 관리자 Object.m 파일

- (IBAction)welcomeNotification:(UIButton *)sender { 
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 
    [center addObserver:self selector:@selector(sendGetRequest:) name:@"myNotification" object:[LPLocationManager sharedManager]]; 
    [center removeObserver:self]; 
    NSLog(@"welcomeNotication triggered"); 

} 

답변

1

당신이 할 방법이 올바르지 않습니다 (표준 파견과 싱글 번 & 초기화하기 방법)

- (void)setCurrentLocation:(CLLocation *)currentLocation 
{ 
    if (!_location) { 
     _location = [[CLLocation alloc] init]; 
    } 
    _location = currentLocation; 
    NSNumber *latitude = [NSNumber numberWithDouble:self.location.coordinate.latitude]; 
    NSNumber *longitude = [NSNumber numberWithDouble:self.location.coordinate.longitude]; 

    NSLog(@"lat %@ & long %@ in notification section", latitude, longitude); 

    NSNotification *notification = [NSNotification notificationWithName:@"myNotification" object:self userInfo:    @{kSetLat: latitude, 
     kSetLong: longitude}]; 


    [[NSNotificationCenter defaultCenter] postNotification:notification]; 
} 

ViewController.m (정원 다양한). 왜 옵저버를 추가 한 다음 즉시 제거해야합니다. 대부분의 경우, viewWillAppearviewWillDisappear 또는 viewDidAppearviewDidDisappear에 옵저버를 추가/제거합니다.

그것은해야 뭔가 같은 : -

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendGetRequest:) name:@"myNotification" object:nil]; 
} 

-(void)viewWillDisappear:(BOOL)animated{ 
    [super viewWillDisappear:animated]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"myNotification" object:nil]; 
}