视图控制器没有得到-shouldAutorotateToInterfaceOrientation:第二次加载时的消息?

时间:2009-09-30 19:04:05

标签: iphone uiviewcontroller rotation

我有UIViewController我用来控制“弹出”视图,以便在整个应用程序中查看图像。它支持自动旋转,因为无论方向如何,它都会自动调整图像大小。这很好用,但只是我第一次初始化和显示视图控制器。当它关闭时,我将从我的视图层次结构中删除UIView并释放视图控制器 - 但是下次我实例化并将其添加到我的视图层次结构时,它会在手机上停止接收-shouldAutorotateToInterfaceOrientation消息旋转。

这是我实例化和显示它的方式:

popupVC = [[PopupVC alloc] init];
[popupVC viewWillAppear:NO];
[[[UIApplication sharedApplication] keyWindow] addSubview:popupVC.view];
[popupVC viewDidAppear:NO];

这是我在完成时删除/释放它的方式:

[popupVC viewWillDisappear:NO];
[popupVC.view removeFromSuperview];
[popupVC viewDidDisappear:NO];
[popupVC release];
popupVC = nil;

我尝试循环浏览[[UIApplication sharedApplication] keyWindow]个子视图,看看我的弹出式视图是不是在顶部,但它总是如此。并且每次都有不同的地址,所以我知道它是视图控制器类的不同实例。

根据要求,以下是loadView的完整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];
}

3 个答案:

答案 0 :(得分:6)

shouldAutorotateToInterfaceOrientation处理程序不是自定义代码响应旋转事件的地方。它的目的只是告诉操作系统您的视图控制器可以旋转。如果您想要自定义代码来处理旋转事件,您应该覆盖 - didRotateFromInterfaceOrientation或其他类似的回调方法之一,具体取决于您的需求:

  • willRotateToInterfaceOrientation:duration:
  • willAnimateRotationToInterfaceOrientation:duration:
  • didRotateFromInterfaceOrientation:
  • willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
  • didAnimateFirstHalfOfRotationToInterfaceOrientation:
  • willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:

请参阅开发人员文档中的Autorotating Views

答案 1 :(得分:6)

我认为这可能是新OS 3.0中的一个错误。解决此问题的方法是在启用beginGeneratingDeviceOrientationNotifications后使用NSNotificationCenter。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
}

-(void) receivedRotate: (NSNotification *) notification {
    DebugLog(@"ORIENTATION CHANGE");

    UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];

        if(interfaceOrientation == UIDeviceOrientationPortrait) {
            self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
            self.view.bounds = CGRectMake(0, 0, 320, 480);
        }
        else if(interfaceOrientation == UIDeviceOrientationLandscapeLeft) {
            self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
            self.view.bounds = CGRectMake(0, 0, 480, 320);
        }
        else if(interfaceOrientation == UIDeviceOrientationLandscapeRight) {
            self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
            self.view.bounds = CGRectMake(0, 0, 480, 320);
        }
}

答案 2 :(得分:0)

我的问题是我得到了我的rootViewController的旋转事件,但是一旦我启动了第二个viewController,除了运动(摇动)和触摸之外它不会获得任何信号。这是与原帖相同的问题 - 它肯定是类似的吗?

我按照上面的建议进行了跟进,我没有正确编码选择器,所以一旦我尝试旋转它就会引发异常。与此同时,我在网上发现了另一个讨论,证实问题是在3.0中引入的。 link text