viewDidLoad无限重新加载

时间:2015-06-16 17:13:40

标签: ios objective-c

我遇到了一个非常奇怪的错误,尽管没有更改任何代码,但今天早上没有发生。我甚至还回到了上周我正在开发的一个我知道正在工作的版本,同样的事情开始发生了。所以正在发生的事情是我的viewDidLoad方法不断重新运行,似乎每当我向我的self.view添加子视图或尝试访问有关self.view的当前子视图的信息时。这种情况无限发生,直到我收到(EXC_BAD_ACCESS代码= 2)错误并且程序崩溃。

我完全不知道如何解决这个问题,所以任何帮助都会非常感激。我将在下面发布我的代码:

-(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到Google Maps页面崩溃。

-(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

我真的迷路了,希望有人知道答案。

谢谢!

3 个答案:

答案 0 :(得分:1)

在访问self.view之前调用[super viewDidLoad]应解决此问题。

请记住,在没有self.view的viewDidLoad中调用[super viewDidLoad]会隐式加载视图。因此,您的viewDidLoad会被多次调用。

答案 1 :(得分: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;
        //......
}

在从nib加载视图之前调用initWithNibName:bundle:方法,并且只在视图控制器的生命周期中调用一次。

答案 2 :(得分:0)

我发现了这个问题,我打电话给我的一个UIView" loadView"这偶然引起了所有问题。我将UIView的名称更改为" activityView"一切正常。