2015-09-01 102 views
0

我想按照這個例子的代碼:iOS 6: How do I restrict some views to portrait and allow others to rotate?的iOS NSInvalidArgumentException:無法識別的選擇發送到實例

我創建的UINavigationController的一個子類,稱爲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 

然後在我的應用程序,我嘗試在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

您需要實際創建一個'CustomNavigationController'而不是'UINavigationController'來創建它,您不能僅僅施放一些東西並讓它神奇地改變類型。 – dan

+0

@丹你能解釋一下,那看起來與我所做的不一樣嗎? – scientiffic

+1

這將取決於你在哪裏創建它。如果您在故事板中創建它,那麼您應該選擇導航控制器並在身份檢查器中設置一個自定義類。如果你在代碼中創建它,然後將'[UINavigationController alloc]'更改爲'[CustomNavigationController alloc]' – dan

回答

1

您必須在身份檢查器中爲導航控制器在故事板中設置爲CustomNavigationController

相關問題