【问题标题】:Getting Warnings With Method That Checks Device Orientation使用检查设备方向的方法获取警告
【发布时间】:2011-09-06 23:22:50
【问题描述】:

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

- (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];
}

警告

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

【问题讨论】:

  • 您收到了哪些警告?
  • Implicit conversion from enumoration type 'UIDeviceOrientation' to differnt enumeration type 'UIInterfaceOrientation

标签: iphone ios cocoa-touch ipad orientation


【解决方案1】:

既然您已经发布了错误,那么问题就很清楚了。这一行:

[[UIDevice currentDevice] orientation];

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

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

【讨论】:

    【解决方案2】:

    如果此代码在 viewController 下,您应该使用 self.interfaceOrientation 来完成工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      相关资源
      最近更新 更多