0

I am trying to follow the code in this example: iOS 6: How do I restrict some views to portrait and allow others to rotate?

I created a subclass of UINavigationController called customNavigationController:

customNavigationController.m

@implementation CustomNavigationController
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if (self.landscapeOK) {
        NSLog(@"all orientation ok");
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    NSLog(@"only portrait orientation");
    return UIInterfaceOrientationMaskPortrait;
}

@end

CustomNavigationController.h

@interface CustomNavigationController : UINavigationController

@property (nonatomic) BOOL landscapeOK;

@end

Then in my app, I try setting the landscapeOK property in viewWillAppear:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [(CustomNavigationController*)[self navigationController] setLandscapeOK:NO];
}

When I run [(CustomNavigationController*)[self navigationController] setLandscapeOK:NO]; I get the error

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setLandscapeOK:]: unrecognized selector sent to instance 0x10091ee00'

What am I doing wrong?

Community
  • 1
  • 1
scientiffic
  • 9,045
  • 18
  • 76
  • 149
  • 1
    You need to actually create a `CustomNavigationController` instead of a `UINavigationController` where you are creating it, you can't just cast something and have it magically change types. – dan Sep 01 '15 at 21:00
  • @dan can you explain how that would look different than what I've done? – scientiffic Sep 01 '15 at 21:09
  • 1
    That would depend on where you are creating it. If you create it in the storyboard then you should select the navigation controller and set a custom class in the identity inspector. If you create it in code then change `[UINavigationController alloc]` to `[CustomNavigationController alloc]` – dan Sep 01 '15 at 21:11
  • @dan thanks, I was missing that critical element! Adding a custom class to my storyboard fixed the problem. If add this as an answer, I will mark it as correct. – scientiffic Sep 01 '15 at 21:17

1 Answers1

1

You have to set the custom class in the identity inspector for your navigation controller in your storyboard to CustomNavigationController.

dan
  • 9,695
  • 1
  • 42
  • 40