ios - 当使用手势关闭呈现的 View Controller 时如何获得通知?

标签 ios swift uiviewcontroller

在某些情况下(iPhone X、iOS 13)可以通过从顶部拉动以手势关闭呈现的 View Controller 。

在这种情况下,我似乎找不到通知呈现 View Controller 的方法。我错过了什么?

我发现的唯一方法是向呈现的 View Controller 的 viewDidDisappear 添加一个委托(delegate)方法。

就像是:

class Presenting: UIViewController, PresentedDelegate {
    func someAction() {
        let presented = Presented()
        presented.delegate = self
        present(presented, animated: true, completion: nil)
    }
    func presentedDidDismiss(_ presented: Presented) {
        // Presented was dismissed
    }
}

protocol PresentedDelegate: AnyObject {
    func presentedDidDismiss(_ presented: Presented)
}

class Presented: UIViewController {
    weak var delegate: PresentedDelegate?

    override func viewDidDisappear(animated: Bool) {
         ...
         delegate?.presentedDidDismiss(self)
    }
}

也可以通过通知来管理它,使用 vc 子类,但仍然不能令人满意。


extension Notification.Name {
    static let viewControllerDidDisappear = Notification.Name("UIViewController.viewControllerDidDisappear")
}

open class NotifyingViewController: UIViewController {

    override open func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        NotificationCenter.default.post(name: .viewControllerDidDisappear, object: self)
    }
}

必须有更好的方法来做到这一点?

最佳答案

从 iOS 13 开始,Apple 为用户引入了一种新方式 dismiss通过从顶部拉下呈现的 View Controller 。可以通过实现 UIAdaptivePresentationControllerDelegate 来捕获此事件。到UIViewController在这种情况下,您正在展示 Presenting Controller 。然后您可以通过 presentationControllerDidDismiss 方法获得有关此事件的通知。 .这是代码示例:-

class Presenting: UIViewController, UIAdaptivePresentationControllerDelegate {

    func someAction() {
        let presented = Presented()
        presented.presentationController?.delegate = self
        present(presented, animated: true, completion: nil)
    }

    func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
        // Only called when the sheet is dismissed by DRAGGING.
        // You'll need something extra if you call .dismiss() on the child.
        // (I found that overriding dismiss in the child and calling
        // presentationController.delegate?.presentationControllerDidDismiss
        // works well).
    }
}

笔记:
  • 此方法仅在通过从顶部滑动而不是程序化 dismiss(animated:,completion:) 时触发。方法。
  • 您不需要任何自定义委托(delegate)或 Notification观察者用于获取用户通过向下滑动关闭 Controller 的事件,因此您可以删除它们。
  • 关于ios - 当使用手势关闭呈现的 View Controller 时如何获得通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62339874/

    相关文章:

    ios - 在更新UI之前出现NSURLConnection sendAsynchronousRequestcompleteHandler日志出现

    ios - Swift 5/Xcode 11 更新后模拟器在动画 block 处卡住

    swift - IOS Facebook SDK : login doesn't return email despite permissions granted

    ios - 在 Swift 中页面 View Controller 内的 View Controller 之间传递数据

    ios - 使用自定义转换时如何处理呈现 UIViewController 中的方向更改

    ios - iOS 上的 Metal : when to read visibilityResultBuffer?

    ios - 如何在 Swift 3 中将 CoreData 日期转换为日期时间

    ios - 如果响应类型并不总是相同,如何在 TableView 单元格上显示动态 API 数据

    ios - 如何创建一个通用函数来加载 View Controller ?

    ios - 如何使用实体的相关有序集对我的 NSFetchedResultsController 进行排序?