당신이 할 방법이 올바르지 않습니다 (표준 파견과 싱글 번 & 초기화하기 방법)
- (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 (정원 다양한). 왜 옵저버를 추가 한 다음 즉시 제거해야합니다. 대부분의 경우, viewWillAppear
및 viewWillDisappear
또는 viewDidAppear
및 viewDidDisappear
에 옵저버를 추가/제거합니다.
그것은해야 뭔가 같은 : -
-(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];
}