2015-06-16 57 views
1

我遇到了一個非常奇怪的錯誤,儘管沒有改變任何代碼,但它並沒有在今天上午發生。我甚至恢復了上週我正在開發的一個構建,我知道這個構建工作正在進行,同樣的事情也開始發生。所以發生了什麼是我的viewDidLoad方法不斷得到重新運行,似乎每當我添加一個子視圖到我的self.view或嘗試訪問有關self.view的當前子視圖的信息。這種情況無限發生,直到出現(EXC_BAD_ACCESS code = 2)錯誤並且程序崩潰。viewDidLoad無限重新加載

我完全不知如何去解決這個問題,所以任何幫助將不勝感激。我會後我下面的代碼:

-(void) viewDidLoad{ 
    [super viewDidLoad]; 
    screenWidth = [UIScreen mainScreen].bounds.size.width; 
    screenHeight = [UIScreen mainScreen].bounds.size.height; 
    startLat = 42.332985; 
    startLong = -71.473913; 
    endLat = 42.327756; 
    endLong = -71.469589; 
    float cameraPosLat = (startLat + endLat)/2.0f; 
    float cameraPosLong = (startLong + endLong)/2.0f; 

    NSLog(@"Test Text"); **//This is a test message that prints everytime viewDidLoad is called again** 


    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:cameraPosLat 
                 longitude:cameraPosLong 
                  zoom:18]; 
    mapView = [[GMSMapView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)]; 
    mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    mapView.mapType = kGMSTypeSatellite; 
    mapView.delegate = self; 
    mapView.myLocationEnabled = YES; 
    [mapView setMinZoom:18 maxZoom:mapView.maxZoom]; 
    **//Program reaches here** 
    [self.view addSubview:mapView]; 
    **//Program does not reach here, program jumps back to top of viewDidLoad** 
    NSLog(@"Please Reach Here"); 
} 

基於EridB的建議,我試圖移動我的代碼到viewWillAppear中的方法,而不是viewDidLoad中,當我做到這一點,從菜單中SEGUE到谷歌地圖頁面的代碼崩潰頁。

-(IBAction)toCars:(id)sender{ 
    NSLog(@"To Cars"); 
    classification = @"car"; 
    [self performSegueWithIdentifier:@"toMap" sender:sender]; **//Crashes here** 
} 

這給出了以下堆棧跟蹤:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010952dc65 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000109181bb7 objc_exception_throw + 45 
    2 CoreFoundation      0x00000001093f2478 -[__NSPlaceholderArray initWithObjects:count:] + 360 
    3 CoreFoundation      0x0000000109451384 +[NSArray arrayWithObjects:count:] + 52 
    4 UIKit        0x0000000109bc583f _UIViewTopDownSubtreeTraversal + 127 
    5 UIKit        0x0000000109ca4588 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 710 
    6 UIKit        0x0000000109ca57ae -[UIViewController _presentViewController:withAnimationController:completion:] + 3079 
    7 UIKit        0x0000000109be068e +[UIView(Animation) performWithoutAnimation:] + 65 
    8 UIKit        0x0000000109ca77ca __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 333 
    9 UIKit        0x0000000109ca7625 -[UIViewController presentViewController:animated:completion:] + 229 
    10 My App      0x00000001071869fe -[DTMenuViewController toCars:] + 126 
    11 UIKit        0x0000000109f197b6 _UIGestureRecognizerSendActions + 262 
    12 UIKit        0x0000000109f18459 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 532 
    13 UIKit        0x0000000109f1d076 ___UIGestureRecognizerUpdate_block_invoke662 + 51 
    14 UIKit        0x0000000109f1cf72 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 254 
    15 UIKit        0x0000000109f12fed _UIGestureRecognizerUpdate + 2796 
    16 UIKit        0x0000000109bb6686 -[UIWindow _sendGesturesForEvent:] + 1041 
    17 UIKit        0x0000000109bb72b2 -[UIWindow sendEvent:] + 666 
    18 UIKit        0x0000000109b7d581 -[UIApplication sendEvent:] + 246 
    19 UIKit        0x0000000109b8ad1c _UIApplicationHandleEventFromQueueEvent + 18265 
    20 UIKit        0x0000000109b655dc _UIApplicationHandleEventQueue + 2066 
    21 CoreFoundation      0x0000000109461431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    22 CoreFoundation      0x00000001094572fd __CFRunLoopDoSources0 + 269 
    23 CoreFoundation      0x0000000109456934 __CFRunLoopRun + 868 
    24 CoreFoundation      0x0000000109456366 CFRunLoopRunSpecific + 470 
    25 GraphicsServices     0x000000010bd9fa3e GSEventRunModal + 161 
    26 UIKit        0x0000000109b68900 UIApplicationMain + 1282 
    27 My App      0x0000000107181f03 main + 99 
    28 libdyld.dylib      0x000000010afda145 start + 1 
    29 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

我真的丟了,希望有人知道這個問題的答案。

謝謝!

+1

對於初學者來說,您應該在方法的頂部調用'[super viewDidLoad]''。 – Dima

+0

嘗試在'viewWillAppear'下移動此代碼並查看結果 – EridB

+0

@Dima添加[super viewDidLoad]不會改變任何內容。 – shadowarcher

回答

1

在訪問self.view之前調用[super viewDidLoad]應該可以解決此問題。

請記住在您的viewDidLoad中調用self.view而不使用[super viewDidLoad]會隱式加載視圖。因此你的viewDidLoad被調用多次。

+0

我加了[super viewDidLoad],它仍然在做同樣的事情。我將編輯我原來的帖子以反映這一點。 – shadowarcher

+0

請發佈VC – Vig

0

您最好在initWithNibName:bundle:中添加子視圖。下面是你應該做的:將畫面從筆尖加載之前

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)bundleOrNil 
{ 
    if ((self = [super initWithNibName:nibNameOrNil bundle:bundleOrNil])) 
    { 
     screenWidth = [UIScreen mainScreen].bounds.size.width; 
     screenHeight = [UIScreen mainScreen].bounds.size.height; 
     startLat = 41.332985; 
     startLong = -73.473913; 
     endLat = 41.327756; 
     //...... 
} 

initWithNibName:bundle:方法被調用,並且僅在視圖控制器的壽命調用一次。

+1

的標題'viewDidUnload'自iOS 5以來未被使用/調用過。您不需要「取消」視圖。只要沒有保留週期/內存泄漏,當視圖控制器被重新分配時,它們將自行清理。 – Dima

+0

好的,謝謝,我想,你總是會從這些論壇中學到新的東西。我會馬上編輯答案。 –

0

我發現了這個問題,我打電話給我的一個UIView的「loadView」,這偶然導致了所有的問題。我將UIView的名稱更改爲「activityView」,一切正常。