【问题标题】:viewWillDisappear and viewDidDisappear never get calledviewWillDisappear 和 viewDidDisappear 永远不会被调用
【发布时间】:2011-08-19 04:05:45
【问题描述】:

我为 PopoverController 创建了自己的类(没有子类化 UIPopoverController)以我想要的方式呈现 ViewController。

CustomPopoverController 不是 UIViewController,而是有一个名为“contentViewController”的 ivar,它是实际显示的 VC。

我实现了我自己的“dismissPopoverAnimated:”来在用户点击 contentViewController 框架之外的任何地方时关闭我的自定义弹出框:

-(void) dismissPopoverAnimated : (BOOL) animated
{
     // dismissalView is the view that intercept the taps outside.
    [self.dismissalView removeFromSuperview];
    self.dismissalView = nil;
    if (animated)
    {
        CGRect newFrame = self.view.frame;
        // When in landscape Mode the width of the screen is actually the "height"
        newFrame.origin.y = [UIScreen mainScreen].bounds.size.width;

        [UIView animateWithDuration:0.5 
                         animations:^{self.view.frame = newFrame;} 
         completion: ^(BOOL finished) {if(finished) [self.contentViewController.view removeFromSuperview];}];
    }
    else 
    {
        [self.contentViewController.view removeFromSuperview];
    }
    isPresented = NO;
    [self.delegate customPopoverDidDismissPopover:self];
}

问题是,即使在任何情况下都调用了removeFromSuperView - 动画与否,当我发布@987654327 时,contentViewController 永远不会收到viewWillDisappearviewDidDisappear 甚至viewDidUnload @;

有人知道为什么吗? 或者更好地了解 viewWill.../viewDid... 方法链以及它们应该被调用的时间。

【问题讨论】:

  • 如果 CustomPopoverController 不是 UIViewController.. 那么什么是 CustomPopoverController? self.contentViewController.view removeFromSuperview]; 我想会在 ContentViewController 中调用 viewWillDisappear
  • @7KV7 - CustomPopoverController 是我自己的类,直接从 NSObject 派生。我的问题是 viewWillDisappear 没有按预期在 contentViewController 中调用。这正是我想要实现的目标。

标签: iphone objective-c ios ipad uiviewcontroller


【解决方案1】:

当你通过 UIView 的方法添加或删除子视图时,它永远不会导致拥有的UIViewController 调用viewWillAppearviewDidAppearviewWillDisappearviewDidDisapper。只有那些被UINavigationController方法管理的viewController,如pushViewController:animated:popViewControllerAnimated:,或presentModelViewController:aniamted:...等。它们会通知控制器视图的状态正在改变。

【讨论】:

  • 这有点令人惊讶,因为文档中没有提到它。你有这方面知识来源的链接吗?
  • 不,我没有链接。我尝试了很多次,因为我的一些视图是由属于另一个 UIViewController 的视图组成的。通过添加另一个 UIViewController 的视图进行视图转换时,需要手动调用 viewWillAppear:animated:、viewWillDisappear:animated 等。因为 UIViewController 不是由 UINavigationController 或其他东西管理的。 UIView 在屏幕上出现或消失,它会调用 willMoveToWindow: 。出现时,参数不为零;当消失时,UIWindow 的参数为 nil。苹果的文件已经说过了。
  • @Toro 以我的经验 viewWillAppear: 即使在手动控制 UIView 时也总是被调用,但 viewWillDisappear: 在这种情况下真的不会被调用。当 viewWillAppear: 未被调用时,你能举个简单的例子吗?
  • 例如有AB UIViewControllers。现在出现Aview,然后调用[A.view addSubView:B.view]viewWillAppearB 是否被调用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 2013-10-12
  • 2012-03-27
  • 2013-08-29
  • 2013-11-03
  • 1970-01-01
相关资源
最近更新 更多