使用相機時切入後臺報錯:Thread1:EXC_BAD_ACCESS(code=1,address=0x1)解決辦法之一

問題是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];

發佈了2 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章