didRotateFromInterfaceOrientation problems

Discussion in 'iOS Programming' started by DennisBlah, Aug 7, 2014.

  1. DennisBlah, Aug 7, 2014
    Last edited: Aug 7, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hi all,

    I'm having trouble with the didRotateFromInterfaceOrientation function.
    It works fine, like I expected to work. However, when I get in this view when my device is already rotated to landscape I'm getting the following error.

    Code:
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
    Code:
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        if(UIDeviceOrientationIsLandscape(fromInterfaceOrientation))
            //Make sure everything is in place
        else
            //Remove bottombar topbar etc etc
    }
    
    How can I make my app taking care of this hiding and unhiding objects from the start, without the didRotate to be crashing?

    For now I have a boolean before I check the 'if(UIDeviceOrientationIsLandscape(fromInterfaceOrientation))'
    Which will be true in the end of viewdidappear, and in viewdidload I check if my statusbar orientation is landscape, then do the same things as in didRotateFromInterfaceOrientation and keep the boolean false.
     
  2. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #2
    You have an array crashing. Did you try setting a break point to see which array is crashing?
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    Hi JohnsonK,

    the stranges thing is.. It really does crash on this didRotateFrom......
    And it only crashes when I enter the view, when my device is already on landscape.

    And like I said with the workaround in viewDidApear it's not crashing, but now I got something else, but I'll figure that one out.

    Still I think it's strange.

    Without the workaround:
    I enter the view in portrait mode => everything fine
    I restart the app, and enter the view in landscape mode => this crash.

    (All previous views don't support landscape mode, maybe thats something I should have told)
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    DennisBlah, you should probably provide the complete code for didRotateFromInterfaceOrientation: since if that's where it's crashing, as you claim, your provided code does not contain the insertObject:atIndex: call that is causing the crash.
     
  5. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #5
    Hi Dejo,

    it does not matter what I put in. But here it goes, this is what I want it to do:

    Code:
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        if(UIDeviceOrientationIsLandscape(fromInterfaceOrientation)) {
            [buttonBarC setHidden: NO];
            [touchViewC setHidden: NO];
    
            if([self showHeader]) {
                NSLog(@"Showing header..");
                [headersScrollerC setHidden: NO];
                [self loadHeader];
                theGallery.frame = CGRectMake(0, headersScrollerC.frame.origin.x + headersScrollerC.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - topViewC.frame.size.height - buttonBarC.frame.size.height - headersScrollerC.frame.size.height);
            } else {
                NSLog(@"Not showing header..");
                theGallery.frame = CGRectMake(0, topViewC.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - topViewC.frame.size.height - buttonBarC.frame.size.height);
            }
        } else {
            [headersScrollerC setHidden: YES];
            [buttonBarC setHidden: YES];
            [touchViewC setHidden: YES];
    
            
            theGallery.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width - topViewC.frame.size.height);
        }
    }
    
    //Here is loadHeader:
    -(void)loadHeader {
        if([galleryappDelegate->currentHeaders count] > 1) {
            [pageControlC setHidden: NO];
        } else {
            [pageControlC setHidden: YES];
        }
        for (id viewToRemove in [headersScrollerC subviews]) {
            [viewToRemove removeFromSuperview];
        }
        
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:
                          [NSString stringWithFormat: @"%@/%@", galleryappDelegate->myApp, [galleryappDelegate->currentHeaders objectAtIndex: galleryappDelegate->currentHeader]]];
        
        UIImage *theImage = [UIImage imageWithContentsOfFile: path];
        UIImageView *myImage = [[UIImageView alloc] initWithImage: theImage];
        [myImage setFrame: CGRectMake(0, 0, headersScrollerC.frame.size.width, headersScrollerC.frame.size.height)];
        myImage.contentMode = UIViewContentModeScaleToFill;
        headersScrollerC.delegate = self;
        [headersScrollerC addSubview: myImage];
        
        [pageControlC setCurrentPage: galleryappDelegate->currentHeader];
    }
    
    //Show header boolean function
    
    -(BOOL)showHeader {
        BOOL result = NO;
        if(galleryappDelegate->currentButton == 0) {
            result = YES;
        } else {
            if([[galleryappDelegate->currentModules objectAtIndex: galleryappDelegate->currentButton] isEqualToString: @"page"]) {
                if(galleryappDelegate->headerOnPages)
                    result = YES;
            } else {
                if(galleryappDelegate->headerOnModules)
                    result = YES;
            }
        }
        return result;
    }
    
    
    As you see I'm not filling in an array or anything. The strangest thing is.


    If I put this in viewDidLoad:
    Code:
    orientationCheck = NO;
        if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight){
            [headersScrollerC setHidden: YES];
            [buttonBarC setHidden: YES];
            [touchViewC setHidden: YES];
            [naviViewC setHidden: YES];
            
            theGallery.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width - topViewC.frame.size.height);
        } else {
            orientationCheck = YES;
        }
        
    
    and I put this orentationCheck boolean in the didRotateFromInterfaceOrientation, it goes fine. However I'm having an other issue but that has nothing to do with this.

    I think it's a big workaround for such simple thing.
     
  6. dejo, Aug 13, 2014
    Last edited: Aug 13, 2014

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Xocde tends not to report such a specific error unless it knows that's the problem. I suspect the issue lies within some other method then. But I can't say for sure. I would suggest putting in a breakpoint and stepping through the code trying to find the line that is causing the crash. Also, the exception is probably providing more details as to where the issue is. (You've only provided us with the exception message.) It's time to put on your debugging hat! :)
     

Share This Page