【问题标题】:How to dismiss second view controller from popup view如何从弹出视图中关闭第二个视图控制器
【发布时间】:2017-06-22 13:31:39
【问题描述】:

我有 2 个视图控制器 AB。视图控制器 A 通过 segue 显示呈现视图控制器 BViewController B 还有一个按钮,可以关闭 B 并显示 A。到这里为止没有问题。

B中完成一些功能后,视图控制器B会弹出一个视图,这个弹出窗口包括重启游戏和关闭游戏按钮。当按下关闭游戏按钮时,视图控制器B 和弹出视图应该被关闭并显示主A 视图控制器。如何做到这一点?谢谢

这里如何呈现弹出视图:

let popupVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popupID") as! PopupViewController
        self.addChildViewController(popupVC)
        popupVC.view.frame = self.view.frame
        self.view.addSubview(popupVC.view)
        popupVC.didMove(toParentViewController: self)

【问题讨论】:

  • 添加您的任何尝试代码和您为实现的目标所做的事情。
  • 您是在尝试 ipad 还是 iPhone ?如果 ipad 弹出视图意味着弹出控制器使用?
  • iPhone 和 iPad。但不是弹出视图。只是在屏幕上玩游戏。
  • 你了解协议吗?在 PopupViewController 中写下协议并在 B 类中实现该方法。在该方法中的 B 类中编写 self.dismiss(animated: true, completion: nil)。

标签: ios swift uiviewcontroller popup viewcontroller


【解决方案1】:

这里是 Swift3 中的一个解决方案。

据我了解,您希望关闭 ViewController B 中显示的弹出窗口并返回 ViewController A。

    let alertController = UIAlertController(title: "alert", message: "tara", preferredStyle: .alert)

    let action = UIAlertAction(title: "dismiss", style: .default) { (UIAlertAction) in

        // For Dismissing the Popup
        self.dismiss(animated: true, completion: nil)

        // Dismiss current Viewcontroller and back to ViewController B
        self.navigationController?.popViewController(animated: true)

    }
    alertController.addAction(action)
    self.present(alertController, animated: true, completion: nil)

【讨论】:

    【解决方案2】:

    实现此目的的常规方法是使用委托。在您的情况下,您必须首先为委托创建一个协议

    protocol PopupViewControllerDelegate {
        func didSelectClose(_ popupVC: PopupViewController)
    }
    

    现在在 PopupViewController 中添加一个变量,用于调用委托的方法

    class PopupViewController: UIViewController {
        var delegate: PopupViewControllerDelegate?
    }
    

    当用户点击弹出窗口中的close 按钮时,您应该调用委托的方法来通知它用户的操作。

    func didClose(sender: Any) {
        delegate?.didSelectClose(self)
    }
    

    现在,您必须在 ViewControllerB 中实现 PopupViewControllerDelegate,如下所示:

    class ViewControllerB: UIViewController, PopupViewControllerDelegate {
        func didSelectClose(_ popupVC: PopupViewController) {
            // dismiss and go to Root View Controller (A)
            dismiss(animated: true) { 
                self.navigationController?.popToRootViewController(animated: true)
            }
        }
    }
    

    如你所见,当 didSelectClose 被调用时,我们关闭弹出窗口并弹出导航堆栈以转到 ViewControllerA

    最后,在呈现 PopupVC 之前,必须将 ViewControllerB 设置为委托

    func showPopup() {
        let popupVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popupID") as! PopupViewController
    
        popupVC.delegate = self
        present(popupVC, animated: true, completion: nil)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 2013-07-25
      • 2019-07-03
      • 1970-01-01
      相关资源
      最近更新 更多