2009-09-30 52 views
3

我有一个UIViewController,我用它来控制在整个应用程序中查看图像的“弹出”视图。它支持自动旋转,因为它会自动调整图像的大小以适合不管方向。这工作完美,但只有我第一次初始化和显示视图控制器。当它关闭时,我从我的视图层次结构中删除UIView,并释放视图控制器 - 但是当我下次实例化并将其添加到我的视图层次结构中时,它将在手机旋转时停止接收-shouldAutorotateToInterfaceOrientation消息。视图控制器没有收到-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]; 
} 
+0

你什么时候设置视图控制器?另外,我不相信你需要明确地调用'viewWillAppear:',除非整个问题都是为了防止动画。 – 2009-09-30 19:24:12

+0

当用户选择要显示的图像时,我在'UITableViewDelegate'方法中设置它。我的'PopupVC -loadView'方法设置它使用的视图和子视图。除非发送消息,否则我发现我的'UITabBarController'无法正常工作,所以明确调用了'-viewWillAppear:'后,我只是习惯了。 – pix0r 2009-09-30 19:33:41

+0

你不应该自己调用这些viewWillAppear/viewDidAppear方法。我认为你需要重新思考你如何初始化和清理你的弹出窗口。 – 2009-10-08 16:57:14

回答

6

shouldAutorotateToInterfaceOrientation处理不是自定义代码来旋转响应事件的地方。目的只是告诉操作系统你的视图控制器可以旋转。如果你想拥有的自定义代码来处理旋转事件,你应该在此改变 - didRotateFromInterfaceOrientation或其他类似的回调方法之一根据您的需要:

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

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

+0

好点 - 虽然我没有回应这里的事件,但我正在以错误的方式调试。感谢您指出了这一点。 – pix0r 2009-09-30 22:06:07

+0

虽然现在我看起来我在'-didRotateFromInterfaceOrientation:'中有一些调试代码,并且它也没有被触发。 – pix0r 2009-09-30 22:08:08

+0

如何让视图控制器覆盖'canBecomeFirstResponder'返回YES,'viewDidAppear:animated'中的'[self becomeFirstResponder]'和'viewWillDisappear:animated'中的'[self resignFirstResponder]'。这有什么区别吗? – 2009-10-01 00:51:43

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); 
     } 
} 
+0

'#define degreesToRadians(x)(M_PI *(x)/ 180.0)' – Barry 2014-07-21 22:57:05

0

我的问题是,我得到的旋转事件对我的RootViewController的,但只要我启动我的第二个的viewController,它不会让除了运动(晃动)和触摸的任何信号。这与原来的帖子是否一样 - 它肯定是类似的?

我跟进了上述建议,并且我没有正确选择编码,因此只要我尝试旋转就会引发异常。同时,我在网上发现了这个其他的讨论,确认问题是在3.0中引入的。 link text

相关问题