UiNavigationController自动旋转不在设备上工作?

时间:2011-11-24 12:03:07

标签: ios xcode uinavigationcontroller nsnotifications autorotate

为什么UINavigationController AutoRotation无法在设备上运行?如何为UinavigationController设置自动旋转属性?

    **BusinessCardAppDelegate.m**

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    { 
           [self.window makeKeyAndVisible];  
        RootView *rView=[[RootView alloc]initWithNibName:@"RootView" bundle:nil];
        self.naviGationController =[[[UINavigationController alloc]initWithRootViewController:rView]autorelease];

        [self.window addSubview:naviGationController.view];
        return YES;
    }

    **RootView.m**

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.title =@"BusinessCard";  
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationFunction:) name:UIDeviceOrientationDidChangeNotification object:nil];

    }

    -(void)orientationFunction:(NSNotification*)notification
    {
        UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
         switch (orientation)
        {
            case UIDeviceOrientationPortrait:
                /* AlertView Show*/
                break;

            case UIDeviceOrientationPortraitUpsideDown:
               /* AlertView Show*/
                break;
            case UIDeviceOrientationLandscapeLeft:
               /* AlertView Show*/
                break;
            case UIDeviceOrientationLandscapeRight:
               /* AlertView Show*/
                break;
            default:
                break;
        }

  }

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

为什么这不适用于设备,但在模拟器中工作正常?我不明白为什么这不起作用? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

[[UIDevice currentDevice] orientation]返回UIDeviceOrientation,而不是UIInterfaceOrientation。

如果您想获得当前的界面方向,可以使用以下内容:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

UIInterfaceOrientation orientation = self.interfaceOrientation; // into UIViewController
相关问题