问题描述
我有一个 UIViewController
,该控件用于控制弹出"视图以查看整个应用程序中的图像.它支持自动旋转,因为它会自动调整图像大小以使其适合于任何方向.这完美地工作了,但是只有在我第一次初始化并显示视图控制器时才如此.当它关闭时,我将从视图层次结构中删除 UIView
并释放视图控制器-但是下次我实例化并将其添加到视图层次结构中时,它将停止接收 -shouldAutorotateToInterfaceOrientation
旋转手机时收到的消息.
I have a UIViewController
that I'm using to control a "pop-up" view for viewing images throughout my application. It supports autorotation, as it automatically sizes the image to fit properly regardless of orientation. This works perfectly, but only the first time I initialize and display the view controller. When it closes, I am removing the UIView
from my view hierarchy and releasing the view controller - but the next time I instantiate and add it to my view hierarchy, it stops receiving the -shouldAutorotateToInterfaceOrientation
messages when the phone is rotated.
这是我实例化并显示它的方式:
This is how I instantiate and display it:
popupVC = [[PopupVC alloc] init];
[popupVC viewWillAppear:NO];
[[[UIApplication sharedApplication] keyWindow] addSubview:popupVC.view];
[popupVC viewDidAppear:NO];
这是我完成后删除/释放它的方式:
this is how I remove/release it when it's finished:
[popupVC viewWillDisappear:NO];
[popupVC.view removeFromSuperview];
[popupVC viewDidDisappear:NO];
[popupVC release];
popupVC = nil;
我尝试遍历 [[UIApplication sharedApplication] keyWindow]
子视图,以查看我的弹出视图是否位于顶部,但始终位于顶部.而且每次都有一个不同的地址,所以我确实知道它是视图控制器类的另一个实例.
I've tried looping through [[UIApplication sharedApplication] keyWindow]
subviews to see if somehow my popup view isn't on top, but it always is. And it has a different address each time so I do know that it's a different instance of the view controller class.
根据要求,这是 PopupVC
中完整的 loadView
方法:
As requested, here is the complete loadView
method from PopupVC
:
- (void)loadView {
UIView *myView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
myView.backgroundColor = self.overlayColor;
myView.autoresizesSubviews = NO;
myView.hidden = YES;
myView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view = myView;
[myView release];
_isVisible = NO;
UIView *myMaskView = [[UIView alloc] initWithFrame:self.view.bounds];
myMaskView.backgroundColor = [UIColor clearColor];
myMaskView.clipsToBounds = YES;
myMaskView.hidden = YES;
myMaskView.autoresizesSubviews = NO;
myMaskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myMaskView];
self.imageMaskView = myMaskView;
[myMaskView release];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
myImageView.center = self.view.center;
myImageView.hidden = NO;
myImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleHeight;
[self.imageMaskView addSubview:myImageView];
self.imageView = myImageView;
[myImageView release];
UIButton *myImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
myImageButton.frame = self.view.frame;
myImageButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleHeight;
[myImageButton addTarget:self action:@selector(clickImage:) forControlEvents:UIControlEventTouchUpInside];
[self.imageMaskView addSubview:myImageButton];
self.imageButton = myImageButton;
UIActivityIndicatorView *myActivityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
myActivityView.hidden = YES;
[self.view addSubview:myActivityView];
myActivityView.center = self.view.center;
self.activityView = myActivityView;
[myActivityView release];
}
推荐答案
shouldAutorotateToInterfaceOrientation
处理程序不是自定义代码响应旋转事件的地方.目的只是告诉操作系统您的视图控制器可以旋转.如果要使用自定义代码来处理旋转事件,则应根据需要覆盖- didRotateFromInterfaceOrientation
或其他类似的回调方法之一:
The shouldAutorotateToInterfaceOrientation
handler isn't the place for custom code to respond to rotation events. It's purpose is just to tell the OS that your view controller can rotate. If you want to have custom code to handle the rotation events you should overide - didRotateFromInterfaceOrientation
or one of the other similar callback methods depending on your needs:
-
willRotateToInterfaceOrientation:duration:
-
willAnimateRotationToInterfaceOrientation:duration:
-
didRotateFromInterfaceOrientation:
-
willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
-
didAnimateFirstHalfOfRotationToInterfaceOrientation:
-
willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
willRotateToInterfaceOrientation:duration:
willAnimateRotationToInterfaceOrientation:duration:
didRotateFromInterfaceOrientation:
willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
didAnimateFirstHalfOfRotationToInterfaceOrientation:
willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
请参见在开发人员文档中自动旋转视图.
这篇关于视图控制器未获取-shouldAutorotateToInterfaceOrientation:第二次加载消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!