iOS NSInvalidArgumentException:发送到实例的无法识别的选择器

时间:2015-09-01 20:43:37

标签: ios objective-c

我正在尝试按照此示例中的代码:iOS 6: How do I restrict some views to portrait and allow others to rotate?

我创建了一个名为 customNavigationController 的UINavigationController的子类:

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

然后在我的应用中,我尝试在 viewWillAppear 中设置landscapeOK属性:

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

当我运行[(CustomNavigationController*)[self navigationController] setLandscapeOK:NO];时,我收到错误

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

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