问题描述:

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

网友答案:

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.

相关阅读:
Top