问题描述
我正在尝试检测设备方向的任何变化,以便我可以更新视图.
I'm trying to detect any device orientation change so that I can update the views.
我想更新视图的方向是纵向还是横向,因此我实现了此方法:
I want to update the views whether the orientation is portrait or landscape, so I implemented this method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
我知道,如果要更新视图以正确显示当前方向,则需要实现以下一些方法:
I know that if I want to update the views to show properly for the current orientation, I will need to implement some of the following methods:
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
– didAnimateFirstHalfOfRotationToInterfaceOrientation:
– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
现在,问题是我不明白为什么在旋转模拟器时这些方法都没有触发.
Now, the problem is that I don't understand why none of these methods get fired when I rotate the simulator.
我也尝试了这段代码:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
但是仍然没有.所以我想知道,模拟器会触发轮播通知吗?如果是,我在做什么错了?
But still nothing. So I'm wondering, does the simulator fire rotation notifications?If yes, what am I doing wrong?
推荐答案
快速摘要,对此进行更改:
[window addSubview:viewController.view];
对此:
[window setRootViewController:viewController];
很抱歉,如果我花了一些时间来犯下我的一个愚蠢的错误.我发现了为什么永远不会调用方法– willRotateToInterfaceOrientation:duration:
的原因.引起我注意的是导航控制器:
I'm sorry if I took somebody's time for a silly mistake of mine. I found out why the method – willRotateToInterfaceOrientation:duration:
would never get called. Something brought my attention to the navigation controller:
我在另一个论坛上找到了这些帖子,然后看了看一下应用程序委托.我的代码如下:
I found these post in another forum and I had a look at the app delegate. My code was as follows:
CGRect bound = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, bound.size.width, bound.size.height)];
TestViewController *viewController = [[TestViewController alloc] init];
[window addSubview:viewController.view];
[viewController release];
[self.window makeKeyAndVisible];
问题是我没有为窗口设置任何视图控制器,但是我只是添加了一个视图.由于急于测试某些东西,所以我犯了一个错误.我不得不像这样修复代码:
The problem was that I was not setting any view controller for the window, but I was just adding a view. It was a mistake I did due to the hurry to test something. I had to fix the code like this:
CGRect bound = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, bound.size.width, bound.size.height)];
TestViewController *viewController = [[TestViewController alloc] init];
[window setRootViewController:viewController];
[viewController release];
[self.window makeKeyAndVisible];
这篇关于查看轮播通知:为什么RotateFromInterfaceOrientation:不会被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!