代码之家  ›  专栏  ›  技术社区  ›  pix0r

视图控制器未获取-shouldautorotatePointerFaceOrientation:messages on second load?

  •  3
  • pix0r  · 技术社区  · 15 年前

    我有一个 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 回复  |  直到 15 年前
        1
  •  6
  •   Jason Jenkins    15 年前

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

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

    Autorotating Views 在开发人员文档中。

        2
  •  6
  •   Jonathan Reyes    15 年前

    我认为这可能是新的OS3.0中的一个bug。解决方法是在打开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);
            }
    }
    
        3
  •  0
  •   mobibob    15 年前

    我的问题是,我得到了rootviewcontroller的旋转事件,但一旦我启动了第二个viewcontroller,它将不会得到任何信号,除了运动(震动)和触摸。这是不是和原来的帖子一样——当然是相似的??

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