2012-04-10 51 views
0

我想保持我的風景。爲此,我使用此代碼,但BAD_ACCESS即將到來。 在這裏,我正在爲攝像機overlayView編寫此代碼。BAD_ACCESS雖然風景取向

-(void)viewDidLoad 
{ 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft]; 
    //Set Notifications so that when user rotates phone, the orientation is reset to landscape. 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    //Refer to the method didRotate: 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(didRotate:) 
       name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 
    [super viewDidLoad]; 
} 

- (void) didRotate:(NSNotification *)notification 
{ 
     //Maintain the camera in Landscape orientation 
     [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft]; 
} 

它爲什麼會給BAD ACCESS?

+0

'orientation'屬性爲只讀。你如何設置它?它應該給出警告。 – beryllium 2012-04-10 11:29:37

+1

就像旁邊一樣 - 如果常量「UIDeviceOrientationDidChangeNotification」存在,則使用值「@」UIDeviceOrientationDidChangeNotification「'是不好的做法,您應該始終使用該常量。它可以讓編譯器告訴你,你是否拼錯了它,以及如果在未來版本的iOS中值發生變化會怎樣? (同意,不太可能在這種情況下,但總是肯定會更好!) – deanWombourne 2012-04-10 11:40:56

回答

4

爲了讓您的觀點的景觀,只是在你的shouldAutorotateToInterfaceOrientation:方法返回NO爲縱向模式,就像tihs:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return UIInterfaceOrientationIsLandscape(interfaceOrientation); 
}