ios 查看旋转通知:为什么 didRotateFromInterfaceOrientation: 没有被调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6111013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
View rotation notifications: why didRotateFromInterfaceOrientation: doesn't get called?
提问by Francesco Puglisi
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.
现在,问题是我不明白为什么当我旋转模拟器时这些方法都没有被触发。
I tried as well this piece of code:
我也试过这段代码:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
But still nothing. So I'm wondering, does the simulator fire rotation notifications? If yes, what am I doing wrong?
但还是什么都没有。所以我想知道,模拟器是否会触发轮换通知?如果是,我做错了什么?
采纳答案by Francesco Puglisi
Quick summary,change this:
快速总结,改变这个:
[window addSubview:viewController.view];
to this:
对此:
[window setRootViewController:viewController];
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:
如果我因为我的一个愚蠢的错误而耽误了别人的时间,我很抱歉。我发现了为什么该方法– willRotateToInterfaceOrientation:duration:
永远不会被调用。有些东西让我注意到了导航控制器:
Only the root view controller's willRotate method is being called. Most likely you have a strange hierarchy of view controllers.
只有根视图控制器的 willRotate 方法被调用。很可能你有一个奇怪的视图控制器层次结构。
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];
回答by Alkimake
You need to add notification observer something like
您需要添加通知观察者之类的
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
and add the method
并添加方法
- (void) didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft)
{
NSLog(@"Landscape Left!");
}
}