使用检查设备方向的方法获取警告
谁能告诉我这段代码有什么问题吗?我试图确定设备所在的方向(纵向或横向)。谢谢。
- (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”的隐式转换
Can anyone tell me what is wrong with this code? I'm trying to determine what orientation (portrait or landscape, the device is in). Thanks.
- (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];
}
Warning
Implicit conversion from enumoration type 'UIDeviceOrientation' to differnt enumeration type 'UIInterfaceOrientation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在您已经发布了错误,问题就清楚了。此行:
返回
UIDeviceOrientation
的实例,而不是UIInterfaceOrientation
的实例。要删除警告并更正代码,您可以将有问题的行更改为:Now that you've posted the error, the problem is clear. This line:
returns an instance of
UIDeviceOrientation
, not an instance ofUIInterfaceOrientation
. To remove the warning and correct the code, you can change the offending line to this:如果此代码位于 viewController 下,您应该使用 self.interfaceOrientation 来完成工作。
If this code is under a viewController you should use self.interfaceOrientation to make the work.