Getting Warnings With Method That Checks Device Orientation
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.backgroun开发者_运维知识库dColor = [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
Now that you've posted the error, the problem is clear. This line:
[[UIDevice currentDevice] orientation];
returns an instance of UIDeviceOrientation
, not an instance of UIInterfaceOrientation
. To remove the warning and correct the code, you can change the offending line to this:
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
If this code is under a viewController you should use self.interfaceOrientation to make the work.
精彩评论