问题描述
请帮帮我。我有方法:
-(void) getCurrentOrientation{
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIInterfaceOrientationPortrait){
NSLog(@"portrait");
} else if(orientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"LandscapeRight");
} else if(orientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"LandscapeLeft");
}
}
但是当我调用这个getCurrentOrientation时抛出viewDidLoad
but when i call this getCurrentOrientation throw viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
[self getCurrentOrientation];
}
NSLog为空。怎么了 ?
我也试试
NSLog is empty. Whats wrong ?I also try
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)) {
NSLog(@"portrait");
} else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
NSLog(@"LandscapeRight");
}
}
但该varint也是空的。
but that varint also empty.
我需要知道哪个ORIENTATION用户启动APP!
请,给我有什么建议。
推荐答案
你的代码是完全正确的,没有问题。
Your code is absolutely correct and there is no problem.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
此声明仅适用于原始设备。
This statement only works in original devices.
但是你想要在模拟器中办理登机手续,你可以查看
However you want to check in simulator you can check as
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
// portrait
} else {
// landscape
}
更新:
我测试过在设备和模拟器中为您服务。最初它只会在viewDidLoad中显示肖像,但您会将设备保持在横向状态。 First Controller将调查shouldAutoRotate方法。
I have tested for you in both device and simulator. Initially it would show portrait only in viewDidLoad though you will keep device in landscape. First Controller will look into shouldAutoRotate method.
你不应该依赖 viewDidLoad
初步定位。您最初应该依赖于shouldAutorotate方法来确切定位。
You should not depend on viewDidLoad
for initial orientation. You should depend on shouldAutorotate method initially for exact orientation.
这篇关于ViewDidLoad中的iphone设备当前方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!