2014-12-05 9 views
-1

나는 장치가 자신의 방향을 변경할 때 인식 아래이 코드를 가지고 있고, 방향이있는 경우 풍경을 왼쪽 또는 오른쪽으로, 그는 다시 세로로 :방향은 두 번째 시간에 변경되지 않는

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    [[NSNotificationCenter defaultCenter] 
    addObserver:self selector:@selector(orientationChanged:) 
    name:UIDeviceOrientationDidChangeNotification 
    object:[UIDevice currentDevice]]; 


- (void) orientationChanged:(NSNotification *)note{ 

    UIDevice * device = note.object; 

    if(device.orientation == UIDeviceOrientationLandscapeRight){ 


     [[UIDevice currentDevice] setValue: 
     [NSNumber numberWithInteger: UIInterfaceOrientationPortrait] 
            forKey:@"orientation"]; 
    } 

    if(device.orientation == UIDeviceOrientationLandscapeLeft){ 


     [[UIDevice currentDevice] setValue: 
     [NSNumber numberWithInteger: UIInterfaceOrientationPortrait] 
            forKey:@"orientation"]; 
    } 

} 

내 프로젝트를 실행하고 내 장치의 방향을 가로 또는 세로로 변경하면이 코드는 방향을 인식하여 세로 방향으로 변경하지만 가로 또는 세로로 다시 장치를 회전하면 코드가 작동하지 않고 장치가 그대로 유지됩니다. 가로 모드에서, 왜? 이 문제를 해결하는 방법은 무엇입니까?

답변

0

은 내가 올바른 오전 알고 있지만 viewWillAppear의 통지를 등록하고 viewWillDisappear 방법으로 등록을 취소하는 더 나은 방법입니다 viewWillAppear

-(void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillAppear] ; 
    [[NSNotificationCenter defaultCenter] 
addObserver:self selector:@selector(orientationChanged:) 
name:UIDeviceOrientationDidChangeNotification 
object:[UIDevice currentDevice]]; 

} 
0

에서 알림을 발사하려고 해달라고. 보기가 표시 될 때만 알림을 받게됩니다.

-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear] ; 
    [[NSNotificationCenter defaultCenter] 
    addObserver:self selector:@selector(orientationChanged:) 
    name:UIDeviceOrientationDidChangeNotification 
    object:[UIDevice currentDevice]]; 
} 

-(void)viewWillDisappear:(BOOL)animated 
{ 
    // Removing observer for lead created notification 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

그리고 UIDeviceOrientationDidChangeNotification 경우

이 문제를 해결하지 않고, 다음과 같은 StatusBarOrientationNotification을 시도 :

처럼 구현합니다. 대신, 전용 API 덤비는의

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 

- (void)statusBarOrientationChange:(NSNotification *)note { 
    UIInterfaceOrientation orientation = [note.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue]; 

    // handle changes 
} 
0

은 앱 스토어에서 거부 얻을, 당신은해야 같은 supportedInterfaceOrientationsshouldAutorotate 같은 표준 방법을 사용하여 컨트롤러에 대한 단순히 잠금 회전.을