检测呈现的视图控制器何时被解除 [英] Detect when a presented view controller is dismissed

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

问题描述

假设我有一个名为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呈现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:^{
//        
//    }];
}

dismissViewControllerAnimated 在VC1中没有被调用。

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 ,而是调用 dismissViewControllerAnimated:completion on < VC2中的code> self.presentingViewController 。这将直接调用你的覆盖。

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的done处理程序设置为: VC2.onDoneBlock = {[VC2 dismissViewControllerAnimated:YES completion:nil]};

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

使用[self presentViewController:VC2 animated:YES completion:nil];

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

正常呈现VC2控制器在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....

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆