0

I have a simple single view app with two storyboards with different layouts for portrait and landscape. The landscape layout is completely different which is why I am using a second storyboard.

When I switch to landscape the view doesn't change and I get the

"Application tried to present a nil modal view controller on target"

` error in the debugger. I have checked that the landscape storyboard is referencing to the CalculatorViewController class.

This is the line of code which generates the error:

[self presentViewController:landscapeViewController animated:YES completion:nil];

Here is the whole method from CalculatorViewController.m:

- (void)orientationChanged:(NSNotification *)notification

{    
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
           !isShowingLandscapeView)        
    {
       // code here to show landscape storyboard
        UIStoryboard *landscapeStoryboard = [UIStoryboard storyboardWithName:@"LandscapeStoryboard" bundle:nil];

        CalculatorViewControllerLandscape *landscapeViewController = [landscapeStoryboard instantiateInitialViewController];
        [self presentViewController:landscapeViewController animated:YES completion:nil];
        isShowingLandscapeView = YES;
    }    
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&             
         isShowingLandscapeView)        
    {        
        [self dismissViewControllerAnimated:YES completion:nil];        
        isShowingLandscapeView = NO;        
    }
}

The Initial View Controller is called CalculatorViewController. The View Controller for the landscape layout is called CalculatorViewControllerLandscape.

This is my first app so I really appreciate any help. I have tried to find a solution in similar questions posted without success.

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
Sinead
  • 25
  • 1
  • 4
  • Don't forget to accept the answer from Gabriele Petronella. It looks like he helped you finally. – JFS Aug 27 '13 at 14:19

2 Answers2

3

It means that landscapeViewController is nil.

This can be cause by either:

  • landscapeStoryboard being nil (most likely because a storyboard named LandscapeStoryboard cannot be found)
  • no initial view controller being indicated in the storyboard.
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • I have double checked the spelling and capitalisation and LandscapeStoryboard definitely exists.CalculatorViewController is set as the initial view controller. – Sinead Aug 27 '13 at 10:49
  • Can you check whether `storyboard` is `nil`? Use a breakpoint or a `NSLog` – Gabriele Petronella Aug 27 '13 at 10:51
  • I didn't have the initial view controller chekc box checked in the LandscapeStoryboard. When I do I get a diffferent error :NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key aButton.' I don't understand this error either. – Sinead Aug 27 '13 at 10:59
  • it means you have a dangling outlet for `aButton`. Check every outlet on `CalculatorViewControllerLandscape` in Interface Builder – Gabriele Petronella Aug 27 '13 at 11:01
  • I put in a breakpoint and it says landscapeStoryboard = (UIStoryboard *)0x07175c40. – Sinead Aug 27 '13 at 11:02
  • Ok, as you already found out it was my second option then: no initial view controller selected. Now you have a different issue, specifically this one: http://stackoverflow.com/questions/3088059/this-class-is-not-key-value-coding-compliant-for-the-key – Gabriele Petronella Aug 27 '13 at 11:03
  • I fixed the outlet problem, realised I need a whole new set of buttons for the landscape view. So now I don't get the error :) but the landscape view is still not being displayed. At the breakpoint I'm still getting landscapeStoryboard = (UIStoryboard *) which I assume means storyboard is nil. I can't see what is wrong with code, have rechecked spelling – Sinead Aug 27 '13 at 11:28
  • if you get something like `landscapeStoryboard = (UIStoryboard *)0x07175c40` it means that it's not `nil`. Is `landscapeViewController` still `nil`? Are you still getting the error exposed in the question? – Gabriele Petronella Aug 27 '13 at 11:30
  • Problem solved by resetting the landscape storyboard class even though it was set correctly and also copy and past the storyboard name into the code even though it was spelt correctly also. wierd but works now. Thanks so much for all your help much appreciated, I had been trying to solve this issue for several hours. Sinead – Sinead Aug 27 '13 at 11:48
  • You are welcome. I'm glad you solved. Don't forget to accept the answer if it helped you ;) – Gabriele Petronella Aug 27 '13 at 11:50
0

You need to make sure that the identifier for the storyboard landscape-view is set to "LandscapeStoryboard". Select the storyboard and check the identifier.

JFS
  • 2,992
  • 3
  • 37
  • 48
  • `LandscapeStoryboard` is the name of the storyboard not of the controller (duh) – Gabriele Petronella Aug 27 '13 at 10:50
  • Now your answer doesn't make sense. Storyboards are looked up by file name, so you don't have to do anything else than picking the right name. There are not identifiers to be set. – Gabriele Petronella Aug 27 '13 at 11:06
  • Actually it works if I remove the identifier also. 2 other things I did was to reset the landscape storyboard class even though it was set correctly and also copy and past the storyboard name into the code even though it was spelt correctly also. wierd but works now. Thank you both for your help. Sinead – Sinead Aug 27 '13 at 11:47
  • @Gabriele Petronella, I usually use `self.ViewLandscapeViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LandscapeViewController"];` which is using identifiers. But in this case you might be right. – JFS Aug 27 '13 at 18:31
  • @Sinead, you really should think about accepting the answer from Gabriele Petronella. He did a great job helping you! – JFS Aug 27 '13 at 18:33