如何在弹出窗口关闭时 运行 在 swift 中的主视图控制器中编写代码
How to run code in your main view controller in swift when a pop up closes
我目前正在编写我的第一个 swift 应用程序。目前有一个 view/view 控制器在应用 运行 时加载,还有一个弹出窗口 window 绑定到一个单独的视图控制器(例如:https://www.youtube.com/watch?v=S5i8n_bqblE)。当我关闭弹出窗口时,我想更新原始视图中的一些内容和 运行 一些代码。但是,func viewDidLoad() 和 func viewDidAppear() 似乎都运行。而且我无法从弹出视图中执行任何操作,因为我无法从中访问主视图控制器中的组件。我该怎么办?
弹出窗口是 "presented modally" 是否有影响?
我假设您有一个 MainViewController,您要从中呈现 PopupVC。你可以在这里使用委托模式。
定义一个 PopupVCDelegate 如下
protocol PopupVCDelegate {
func popupDidDisappear()
}
在您的 PopupVC 中定义类型为 PopupVCDelegate
的 delegate
属性。并在closePopup
方法中,调用委托方法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
的 class 都可以在调用 closePopup
时收到回调。因此,让您的 MainViewController 采用此委托。
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
}
}
另一种方法是在 closePopup
上通过 NSNotificationCenter
触发通知,并在 MainViewController 中添加一个观察者来监听该通知。但不建议在这种情况下使用。
编辑
正如您要求的 NSNotificationCenter 方法。请按照以下方式更改您的 classes
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)
}
}
我目前正在编写我的第一个 swift 应用程序。目前有一个 view/view 控制器在应用 运行 时加载,还有一个弹出窗口 window 绑定到一个单独的视图控制器(例如:https://www.youtube.com/watch?v=S5i8n_bqblE)。当我关闭弹出窗口时,我想更新原始视图中的一些内容和 运行 一些代码。但是,func viewDidLoad() 和 func viewDidAppear() 似乎都运行。而且我无法从弹出视图中执行任何操作,因为我无法从中访问主视图控制器中的组件。我该怎么办?
弹出窗口是 "presented modally" 是否有影响?
我假设您有一个 MainViewController,您要从中呈现 PopupVC。你可以在这里使用委托模式。
定义一个 PopupVCDelegate 如下
protocol PopupVCDelegate {
func popupDidDisappear()
}
在您的 PopupVC 中定义类型为 PopupVCDelegate
的 delegate
属性。并在closePopup
方法中,调用委托方法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
的 class 都可以在调用 closePopup
时收到回调。因此,让您的 MainViewController 采用此委托。
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
}
}
另一种方法是在 closePopup
上通过 NSNotificationCenter
触发通知,并在 MainViewController 中添加一个观察者来监听该通知。但不建议在这种情况下使用。
编辑
正如您要求的 NSNotificationCenter 方法。请按照以下方式更改您的 classes
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)
}
}