你能检测到 UIViewController 何时被解除或弹出吗? [英] Can you detect when a UIViewController has been dismissed or popped?

查看:33
本文介绍了你能检测到 UIViewController 何时被解除或弹出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我的一个视图控制器被解除/弹出/卸载时,我需要在共享资源中执行一些清理吗?这可能是当用户点击单个屏幕上的后退按钮时,或者如果调用 popToRootViewController(在这种情况下,我最好能够清除弹出的每个控制器.)

I have some cleanup that needs to be performed in a shared resource any time one of my view controllers is dismissed/popped/unloaded? This could either be when the user hits the back button on that individual screen or if a call to popToRootViewController is made (in which case, I would ideally be able to clear up every controller that was popped.)

显而易见的选择是在 viewDidUnload 中执行此操作,但当然,这不是卸载的工作方式.有没有办法捕获所有从堆栈中删除 ViewController 的情况?

The obvious choice would be to do this in viewDidUnload, but of course, that isn't how unload works. Is there a way to catch all cases to where the ViewController is removed from the stack?

忘记提及我正在使用 Xamarin 执行此操作,因此可能会也可能不会影响答案.

edit:Forgot to mention that I am doing this using Xamarin so that may or may not impact the answers.

推荐答案

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    if (isBeingDismissed() || isMovingFromParentViewController()) {
        // clean up code here
    }
}

为 swift 4/5 编辑

EDIT for swift 4/5

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    if (isBeingDismissed || isMovingFromParent) {
        // clean up code here
    }
}

这篇关于你能检测到 UIViewController 何时被解除或弹出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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