問題是ARC錯誤release的對象再次被release。
程序切入後臺時不會走disapear方法
參考了http://songzengbin.github.io後總結如下
加入監聽監聽切入後臺或進入前臺,手動執行disappear
在init中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBackgorund:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
#pragma mark application enterBackgorund enterForeground
-(void)enterBackgorund:(NSNotification *)notif
{
[self viewWillDisappear:YES];
}
-(void)enterForeground:(NSNotification *)notif
{
[self viewWillAppear:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[self.mapView viewWillAppear];
self.isMapViewShowing = !self.mapView.hidden;
self.mapView.delegate = self; // 此處記得不用的時候需要置nil,否則影響內存的釋放
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.mapView viewWillDisappear];
self.isMapViewShowing = NO;
self.mapView.delegate = nil; // 不用時,置nil
}
在dealloc中
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];