由於NavigationController的原因,我有一個ViewController(帶有MKMapView),它被推入。所以我有一個帶有「後退」按鈕的NavBar。 單擊該後退按鈕,我得到一個錯誤:iPhone - 正在釋放類的實例...而關鍵值觀察仍然註冊了它
2010-01-11 18:05:35.273 TestApp[147:207] An instance 0x1758f0 of class MKUserLocation is being deallocated while key value observers are still registered with it. Observation info is being leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: ( Context: 0x0, Property: 0x17d600> ) Program received signal: 「EXC_BAD_ACCESS」.
我與觀察員執行viewDidLoad方法:
- (void)viewDidLoad {
mapView = (MKMapView*)self.view;
mapView.delegate = self;
mapView.mapType = MKMapTypeHybrid;
mapView.showsUserLocation = YES;
// ...
[mapView.userLocation addObserver:self forKeyPath:@"location" options:0 context:NULL];
[super viewDidLoad];
}
我的dealloc:
- (void)dealloc {
[groupId release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
誰能告訴我這裏有什麼問題嗎?我單擊NavBar中的後退按鈕,然後進入dealloc方法,然後切換回較高的ViewController並引發此錯誤。
非常感謝&最好的問候。
此作品非常棒!沒有錯誤了。 – Tim 2010-01-11 18:33:28
更具體地說,您將兩種不同的通知模式組合在一起:NSNotifications和KVO。 (儘管Brad的解決方案是正確的!) – 2010-11-18 03:22:19