使用检查设备方向的方法获取警告

Jon*_*Jon 0 iphone cocoa-touch orientation ipad ios

谁能告诉我这段代码有什么问题?我正在尝试确定哪个方向(纵向或横向,设备所在).谢谢.

- (void)checkOrientation
{
    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

    switch (orientation) 
    {
        case UIInterfaceOrientationPortrait:
            self.tableView.backgroundColor = [UIColor whiteColor];
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            self.tableView.backgroundColor = [UIColor whiteColor];
            break;
        case UIInterfaceOrientationLandscapeLeft:
            self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background2.png"]]; 
            break;
        case UIInterfaceOrientationLandscapeRight:
            self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background2.png"]]; 
            break;
    }
    [self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)

警告

从枚举类型'UIDeviceOrientation'到不同枚举类型'UIInterfaceOrientation的隐式转换

Pen*_*One 5

既然您已发布错误,问题就很明显了.这一行:

[[UIDevice currentDevice] orientation];
Run Code Online (Sandbox Code Playgroud)

返回实例UIDeviceOrientation,而不是实例UIInterfaceOrientation.要删除警告并更正代码,您可以将违规行更改为:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
Run Code Online (Sandbox Code Playgroud)