本文介绍了检测何时关闭呈现的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个名为 VC2 的视图控制器类的实例.在 VC2 中,有一个取消"按钮会自行关闭.但是当取消"按钮被触发时,我无法检测或接收任何回调.VC2 是一个黑匣子.

Let's say, I have an instance of a view controller class called VC2. In VC2, there is a "cancel" button that will dismiss itself. But I can't detect or receive any callback when the "cancel" button got trigger. VC2 is a black box.

视图控制器(称为 VC1)将使用 presentViewController:animated:completion: 方法呈现 VC2.

A view controller (called VC1) will present VC2 using presentViewController:animated:completion: method.

当 VC2 被解雇时,VC1 必须检测哪些选项?

What options does VC1 have to detect when VC2 was dismissed?

根据@rory mckinnel 的评论和@NicolasMiari 的回答,我尝试了以下操作:

From the comment of @rory mckinnel and answer of @NicolasMiari, I tried the following:

在 VC2 中:

-(void)cancelButton:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:^{

    }];
//    [super dismissViewControllerAnimated:YES completion:^{
//
//    }];
}

在 VC1 中:

//-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
- (void)dismissViewControllerAnimated:(BOOL)flag
                           completion:(void (^ _Nullable)(void))completion
{
    NSLog(@"%s ", __PRETTY_FUNCTION__);
    [super dismissViewControllerAnimated:flag completion:completion];
//    [self dismissViewControllerAnimated:YES completion:^{
//
//    }];
}

但是没有调用 VC1 中的 dismissViewControllerAnimated.

But the dismissViewControllerAnimated in the VC1 was not getting called.

推荐答案

根据文档,呈现控制器负责实际解雇.当被呈现的控制器解散自己时,它会要求呈现者为它做这件事.因此,如果您在 VC1 控制器中覆盖dismissViewControllerAnimated,我相信当您在 VC2 上点击取消时它会被调用.检测关闭,然后调用将执行实际关闭的超类版本.

According to the docs, the presenting controller is responsible for the actual dismiss. When the presented controller dismisses itself, it will ask the presenter to do it for it. So if you override dismissViewControllerAnimated in your VC1 controller I believe it will get called when you hit cancel on VC2. Detect the dismiss and then call the super classes version which will do the actual dismiss.

从讨论中发现这似乎不起作用.与其依赖底层机制,不如在VC2本身上调用dismissViewControllerAnimated:completion,而是在VC2中的self.presentingViewController上调用dismissViewControllerAnimated:completion.这将直接调用您的覆盖.

As found from discussion this does not seem to work. Rather than rely on the underlying mechanism, instead of calling dismissViewControllerAnimated:completion on VC2 itself, call dismissViewControllerAnimated:completion on self.presentingViewController in VC2. This will then call your override directly.

更好的方法是让 VC2 提供一个在模态控制器完成时调用的块.

A better approach altogether would be to have VC2 provide a block which is called when the modal controller has completed.

所以在 VC2 中,提供一个名为 onDoneBlock 的块属性.

So in VC2, provide a block property say with the name onDoneBlock.

在 VC1 中显示如下:

In VC1 you present as follows:

  • 在VC1中,创建VC2

  • In VC1, create VC2

将 VC2 的完成处理程序设置为:VC2.onDoneBlock={[VC2dismissViewControllerAnimated:YES completion:nil]};

Set the done handler for VC2 as: VC2.onDoneBlock={[VC2 dismissViewControllerAnimated:YES completion:nil]};

使用 [self presentViewController:VC2 animation:YES completion:nil] 正常呈现 VC2 控制器;

Present the VC2 controller as normal using [self presentViewController:VC2 animated:YES completion:nil];

在VC2中,在取消目标动作中调用self.onDoneBlock();

In VC2, in the cancel target action call self.onDoneBlock();

结果是VC2告诉任何提出它的人它已经完成.您可以扩展 onDoneBlock 以具有指示模态是否完成、取消、成功等的参数......

The result is VC2 tells whoever raises it that it is done. You can extend the onDoneBlock to have arguments which indicate if the modal comleted, cancelled, succeeded etc....

这篇关于检测何时关闭呈现的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 21:14