isBeingDismissed未在viewWillDisappear中设置:

时间:2012-04-20 14:43:48

标签: iphone ios uiviewcontroller modalviewcontroller

我有一些代码要清理我的viewWillDisappear:,我只想在视图移回父视图控制器时使用。

- (void)viewWillDisappear:(BOOL)animated
{
    if ([self isMovingFromParentViewController] || [self isBeingDismissed]) {
        NSLog(@"isMovingFromParentViewController or isBeingDismissed");
        // clean up
    }
    [super viewWillDisappear:animated];
}

视图可以通过两种方式呈现:它可以由导航控制器推动,或者作为模态视图控制器(来自同一导航控制器)呈现。如果它被按下,然后弹出(按下后退按钮),我的清理代码就会运行。如果它被呈现为模态视图控制器,然后被解雇,则代码不会运行。

我这样解雇:

[rootViewController dismissModalViewControllerAnimated:YES];

我的问题是:当我解除我的视图控制器时为什么不设置isBeingDismissed

5 个答案:

答案 0 :(得分:21)

如果这是被解雇的模态导航控制器中的第一个视图控制器,则从self.isBeingDimissed()调用viewWillDisappear:会返回false

但是,由于整个导航控制器正在被解雇,实际工作的是self.navigationController?.isBeingDismissed(),它返回true

答案 1 :(得分:6)

正如@Yuval Tal所提到的,当您解除嵌入导航控制器内部的控制器时,此标志不起作用。这是我使用的扩展程序:

extension UIViewController 
{
    var isAboutToClose: Bool {
        return self.isBeingDismissed ||      
               self.isMovingFromParentViewController ||          
               self.navigationController?.isBeingDismissed ?? false
    }
}

当标准.isBeingDismissed无法正常工作时,如果发现另一种情况,可以轻松扩展。如果你发现,让我们在评论中告诉我。

答案 2 :(得分:4)

您的问题是如何解雇您的模态视图。 rootViewController是如何定义的?

当我致电[self dismissModalViewControllerAnimated:YES]时,[self isBeingDismissed]评估为真。

当我调用[parentViewController dismissModalViewControllerAnimated:YES]时,[self isBeingDismissed]的计算结果为true,其中parentViewController是呈现模态视图的UIViewController(注意:不是UINavigationController)。

答案 3 :(得分:1)

如果您有可能来到这里尝试在非模态显示的视图控制器上使用isBeingDismissed,您可以随时查看topViewController的{​​{1}}属性,例如:< / p>

navigationController

答案 4 :(得分:0)

        viewController.isBeingPresented == NO;
        [rootVC presentViewController:viewController animated:NO completion:^{
            viewController.isBeingPresented == NO;
            viewController.isBeingDismissed == NO;
            [viewController dismissViewControllerAnimated:NO completion:^{
                viewController.isBeingDismissed == NO;
            }];
            viewController.isBeingDismissed == NO;    // is not work
        }];
        viewController.isBeingPresented == YES;     // is work
        viewController.isBeingPresented == NO;
        [rootVC presentViewController:viewController animated:NO completion:^{
            viewController.isBeingPresented == NO;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                viewController.isBeingDismissed == NO;
                [viewController dismissViewControllerAnimated:NO completion:^{
                    viewController.isBeingDismissed == NO;
                }];
                viewController.isBeingDismissed == YES;    // is work
            });
        }];
        viewController.isBeingPresented == YES;     // is work