2015-05-22 50 views
2

我有個簡單地查看控制器用的MKMapView對象的內部,以這種方式定義:的MKMapView與iOS 8碰撞

類聲明:

@interface GeoViewController : UIViewController <MKMapViewDelegate> 

@property (nonatomic, strong) MKMapView* viewMap; 

@end 

實現:

@implementation GeoViewController 

- viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    viewMap = [[MKMapView alloc] init]; 
    MKCoordinateRegion worldRegion = MKCoordinateRegionForMapRect(MKMapRectWorld); 
    [viewMap setRegion:worldRegion animated:YES]; 
    viewMap.pitchEnabled = YES; 
    viewMap.showsUserLocation = YES; 
    viewMap.delegate = self; 
    [self.view addSubview:viewMap]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    viewMap.showsUserLocation = NO; 
    self.viewMap.delegate = nil; 
    [self.viewMap removeFromSuperview]; 
} 

- (void)resizeForOrientation:(BOOL)isLandscape withFrame:(CGRect)frame { 
    [super resizeForOrientation:isLandscape withFrame:frame]; 
    const CGRect fullFrame = CGRectMake(0, 0, frame.size.width, frame.size.height); 
    viewMap.frame = fullFrame; 
} 

@end 

首先我介紹視圖控制器工作的時間。然後,我瀏覽我的應用程序,如果我回來GeoViewController,崩潰:

  • 在控制檯上沒有消息錯誤
  • main.m文件被打開,並報告「主題1:EXC_BAD_ACCESS」
  • 上調試導航(左)我得到線程1錯誤報告:

    EAGLContext_renderbufferStorageFromDrawable(EAGLContext *,objc_selector *,無符號整型,ID)

現在,谷歌搜索了很多,我不能給我找一個工作的解決方案:

我也嘗試viewDidDisappear,但沒辦法

[EAGLContext setCurrentContext:nil]; 

似乎崩潰了此行:

viewMap.frame = fullFrame; 

任何建議將不勝感激!

更新

我收到其他錯誤某個時候在我的測試:

OpenGL的線程衝突檢測

+0

可能是不相關的,但由於'viewMap'是一個屬性,你應該使用'的self.viewMap = ...'而不是僅僅'視圖地圖= ...'分配它。所以'viewMap = [[MKMapView alloc] init];'應該是'self.viewMap = [[MKMapView alloc] init];'。 – Anna

回答

0

我覺得這行是一個導致了問題

[self.viewMap removeFromSuperview]; 

因爲如果你說你只是導航回視圖,viewDidLoad將不會再被調用。這就是爲什麼它說「線程1:EXC_BAD_ACCESS」,因爲你訪問一個發佈的對象。它在viewMap.frame = frame上崩潰,因爲你正在訪問已經被釋放的viewMap對象,因爲你從它的superView中移除它。

+0

與此同時我已經移動viewDidAppear:(布爾)動畫,它是一樣的。 –

+0

嘗試viewWillAppear:(BOOL)動畫 –

+0

或者你可以避免從它的superView中刪除self.viewMap。由於您正在使用ARC,因此self.viewMap將在不再使用時自動發佈。 –

0

viewDidAppear在您將控制器開啓時以及將另一個控制器推到頂部時調用。因此,在您導航的那一刻,您正在移除地圖。這同樣適用於viewWillDisappear,因爲當您將新控制器放在頂部並且最終關閉控制器時,它將被調用。

只需更改控制器第一次出現時添加地圖的功能,並且只能在控制器從導航控制器彈出時進行刪除。喜歡的東西:

- viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    if (self.isMovingToParentViewController){ 
     viewMap = [[MKMapView alloc] init]; 
     MKCoordinateRegion worldRegion = MKCoordinateRegionForMapRect(MKMapRectWorld); 
     [viewMap setRegion:worldRegion animated:YES]; 
     viewMap.pitchEnabled = YES; 
     viewMap.showsUserLocation = YES; 
     viewMap.delegate = self; 
     [self.view addSubview:viewMap]; 
    } 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 

    // If being popped off, remove the map view. Otherwise 
    // leave it alone as we need it when the user navigates 
    // back 
    if (self.isMovingFromParentViewController){ 
    viewMap.showsUserLocation = NO; 
    self.viewMap.delegate = nil; 
    [self.viewMap removeFromSuperview]; 
    } 
} 

}

這可確保地圖是本作的控制器,應確保所有訪問它在任何時候都有效的生命週期的持續時間。

+0

不幸的是這不能解決我的問題.. –

+0

@Velthune我注意到你編輯你的問題,並將地圖創建移動到'viewDidAppear'。你也應該提供一個警衛,所以你只能在控制器第一次被推動時創建。查看更新的答案。如果你一直添加/刪除,有一段時間你可能沒有地圖。因此,請在首次展示時創建它,並在彈出時將其刪除,如更新後的答案中所示。 –

+0

我會試試看! –

4

EAGLContext_renderbufferStorageFromDrawable(EAGLContext*, objc_selector*, unsigned int, id) 

藏漢了EXC_ARM_DA_ALIGN錯誤,使用的MKMapView時。

此提問中提到的解決方法:Adding google maps as subview crashes iOS app with exc_bad 爲我工作。

禁用我的目標方案中的「GPU幀捕獲」功能的伎倆。

+0

是的,它適合我! – ziggear

+0

它正在工作。尼斯 –

2

產品 - >編輯方案 - >運行 - >選項 - >將GPU幀捕獲設置爲禁用。

Refetence Link