当弹出窗口关闭时,如何快速在主视图控制器中运行代码 [英] How to run code in your main view controller in swift when a pop up closes

查看:76
本文介绍了当弹出窗口关闭时,如何快速在主视图控制器中运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写我的第一个 swift 应用程序.目前有一个视图/视图控制器在应用程序运行时加载,以及一个绑定到单独视图控制器的弹出窗口(如下所示:https://www.youtube.com/watch?v=S5i8n_bqblE).当我关闭弹出窗口时,我想更新原始视图上的一些内容并运行一些代码.但是, func viewDidLoad() 和 func viewDidAppear() 似乎都没有运行.而且我无法从弹出视图中执行任何操作,因为我无法从中访问主视图控制器中的组件.我该怎么办?

I'm currently writing my first swift app. Currently there is one view/view controller that loads when the app is run as well as a popup window tied to a separate view-controller (like so: https://www.youtube.com/watch?v=S5i8n_bqblE). When I close the pop-up I want to update several things on my original view and run some code. However, neither func viewDidLoad() nor func viewDidAppear() seems to run. And I can't do anything from the pop-up view since I don't have access to the components in the main view-controller from it. What should I do?

如果这有区别,弹出窗口是模态呈现的"?

The pop-up is "presented modally" if that makes a difference?

推荐答案

我假设您有一个 MainViewController,您可以从中呈现 PopupVC.您可以在此处使用委托模式.
定义一个 PopupVCDelegate 如下

I'm assuming you have a MainViewController from which you're presenting the PopupVC. You can use delegate pattern here.
Define a PopupVCDelegate as follow

protocol PopupVCDelegate {
    func popupDidDisappear()
}

在您的 PopupVC 中定义 PopupVCDelegate 类型的 delegate 属性.而在closePopup方法中,调用委托方法popupDidDisappear

In your PopupVC define a delegate property of type PopupVCDelegate. And in the closePopup method, call the delegate method popupDidDisappear

class PopupVC: UIViewController {
    public var delegate: PopupVCDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func closePopup(_ sender: Any) {
        dismiss(animated: true, completion: nil)
        delegate?.popupDidDisappear()
    }
}

现在任何采用此delegate 的类都可以在调用closePopup 时接收回调.所以让你的 MainViewController 采用这个委托.

Now any class that adopts this delegate will be able to receive the callback when the closePopup is called. So make your MainViewController to adopt this delegate.

class MainViewController: UIViewController, PopupVCDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func showPopup() {
        let popupViewController = //Instantiate your popup view controller
        popupViewController.delegate = self
        //present your popup
    }

    func popupDidDisappear() {
        //This method will be called when the popup is closed
    }
}

另一种方法是通过 NSNotificationCenterclosePopup 上触发通知,并在 MainViewController 中添加一个观察者来监听该通知.但在这种情况下不建议这样做.

Another way is to fire a notification through NSNotificationCenter on closePopup and add an observer in MainViewController to listen to that notification. But it is not recommended in this scenario.

正如您所要求的 NSNotificationCenter 方法.请按照以下方式更改您的课程

As you have asked for the NSNotificationCenter method. Please change your classes as follow

class PopupVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func closePopup(_ sender: Any) {
        dismiss(animated: true, completion: nil)

        NotificationCenter.default.post(name: NSNotification.Name("notificationClosedPopup"), object: nil)
    }
}  
class MainViewController: UIViewController, PopupVCDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(onPopupClosed), name: NSNotification.Name(rawValue: "notificationClosedPopup"), object: nil)
    }

    @objc func onPopupClosed() {
        //This method will be called when the popup is closed
    }

    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "notificationClosedPopup"), object: nil)
    }
}

这篇关于当弹出窗口关闭时,如何快速在主视图控制器中运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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