You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在视图控制器中监听PopupDialog的关闭通知?

解决PopupDialog弹窗关闭的监听问题

嘿,我明白你遇到的问题了——用PopupDialog库弹出自定义VC后,想在发起弹窗的VC里监听弹窗关闭,但重写dismiss方法没触发对吧?这是因为PopupDialog的弹窗是由它内部的容器控制器管理的,你在发起VC里重写的dismiss根本不会被调用到。

给你几个靠谱的解决方案,按推荐程度排序:


1. 用PopupDialog自带的dismissCompletion回调(最推荐)

这个是库本身提供的监听弹窗关闭的方法,最直接也最省心。不管弹窗是通过按钮关闭、手势关闭还是自定义VC里调用dismiss,这个闭包都会触发:

let ratingVC = PopupViewController(nibName: "PopupViewController", bundle: nil)
ratingVC.apiKey = self.apiKey
ratingVC.accountNumberString = accountNumberString

let popup = PopupDialog(viewController: ratingVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)

// 添加弹窗关闭后的回调逻辑
popup.dismissCompletion = {
    // 这里写你要执行的代码,比如刷新数据、更新UI等
    print("弹窗已经完全关闭啦!")
}

ratingVC.presentedPopup = popup
self.present(popup, animated: true, completion: nil)

2. 在自定义VC里用闭包通知发起VC

如果需要在自定义的PopupViewController里做一些额外操作后再通知发起VC,这种方法更灵活。首先在你的PopupViewController里定义一个闭包属性:

class PopupViewController: UIViewController {
    // 定义一个闭包,用来通知外部弹窗已关闭
    var onPopupDismissed: (() -> Void)?
    
    // 比如你在VC里有个关闭按钮的方法
    @IBAction func closeButtonTapped(_ sender: UIButton) {
        self.dismiss(animated: true) {
            // 弹窗关闭完成后触发闭包
            self.onPopupDismissed?()
        }
    }
    
    // 如果是通过手势或其他方式关闭,重写dismiss方法触发闭包
    override func dismiss(animated flag: Bool, completion: (() -> Void)?) {
        super.dismiss(animated: flag) {
            self.onPopupDismissed?()
            completion?()
        }
    }
}

然后在发起VC创建ratingVC的时候,给这个闭包赋值:

let ratingVC = PopupViewController(nibName: "PopupViewController", bundle: nil)
ratingVC.apiKey = self.apiKey
ratingVC.accountNumberString = accountNumberString

// 设置弹窗关闭的回调
ratingVC.onPopupDismissed = {
    print("自定义VC通知我弹窗关闭了!")
    // 这里处理你的业务逻辑
}

let popup = PopupDialog(viewController: ratingVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)
ratingVC.presentedPopup = popup
self.present(popup, animated: true, completion: nil)

3. 用NotificationCenter发送通知(适合跨多场景)

如果你的弹窗关闭需要通知多个无关的VC,这种方法可以用,但尽量少用,避免代码耦合。

首先在发起VC里监听通知:

override func viewDidLoad() {
    super.viewDidLoad()
    // 添加通知监听
    NotificationCenter.default.addObserver(self, selector: #selector(handlePopupDismissed), name: NSNotification.Name("PopupDialogDismissed"), object: nil)
}

@objc private func handlePopupDismissed() {
    print("收到弹窗关闭的通知啦!")
    // 处理逻辑
}

// 记得在销毁时移除监听,避免内存泄漏
deinit {
    NotificationCenter.default.removeObserver(self)
}

然后在PopupViewController里,当弹窗关闭时发送通知:

override func dismiss(animated flag: Bool, completion: (() -> Void)?) {
    super.dismiss(animated: flag) {
        // 发送通知
        NotificationCenter.default.post(name: NSNotification.Name("PopupDialogDismissed"), object: nil)
        completion?()
    }
}

内容的提问来源于stack exchange,提问作者DevWithZachary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:41:02