关闭弹出窗口后如何调用函数?

时间:2020-06-01 02:47:01

标签: ios swift completionhandler

我希望有人可以给初学者一些指导,我的问题是:

我有一个弹出窗口 我用mainViewController的segue打开了这个弹出窗口 我想在关闭弹出框时在mainViewController中触发一个函数 在弹出窗口内,我有一个按钮,用于关闭带有以下代码的弹出窗口:

@IBAction func closeButton(_ sender: Any) {

        dismiss(animated: true, completion: {checkIfPopoverDismissed()}) // I want to trigger this function in the mainView
    }

在mainViewController中,我具有以下功能,希望由上述弹出框的关闭触发

 func checkIfPopoverDismissed()
    {
      print("the function is triggered")
    }

因此,我得到了:

 Use of unresolved identifier 'checkIfPopoverDismissed'

因为显然checkIfPopoverDismissed()在mainViewController中,而在弹出视图中不存在。

有什么主意我可以简单地完成这项工作吗?

5 个答案:

答案 0 :(得分:1)

最好的方法是在viewWillAppear的{​​{1}}中调用它。方法如下:

MainViewController

注意:如果您不希望每次出现class MainViewController: UIViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) checkIfPopoverDismissed() } } 时都调用此函数,则可以检查是否显示弹出窗口,并在出现弹出窗口时进行切换,并仅调用此功能当条件满足时。这是您实现此目标的方法:

MainViewController

答案 1 :(得分:1)

理想情况下,您应该编写一个委托以从警报或弹出式View控制器到主视图控制器调用方法。

下面是代码示例:

// Create a protocol with set of methods
protocol AlertVCDelegate: class {
    func checkIfPopoverDismissed()
}

class AlertVC: UIViewController {
    // Create var to hold the delegate
    // Make it weak to avoid reference cycle
    weak var delegate: AlertVCDelegate!

    @IBAction func closeButton(_ sender: Any) {

        dismiss(animated: true, completion: {
            // Trigger the function on the main view controller
            // through delegation
            self.delegate.checkIfPopoverDismissed()
        })
    }
}

// Conform to the Alert delegte
class MainViewController: UIViewController, AlertVCDelegate {
    let alertControllerObj = AlertVC()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Set the delegate
        // Not necessary to do it from viewWillAppear
        // Set the delegate wherever you create a instance for pop-over
        alertControllerObj.delegate = self
    }

   // Implement the method defined in the protocol
   func checkIfPopoverDismissed()
   {
     print("the function is triggered")
   }
}

由于您是初学者,如果您想了解更多有关代表的信息,那么这里是一个不错的简单博客: https://medium.com/@astitv96/passing-data-between-view-controllers-using-delegate-and-protocol-ios-swift-4-beginners-e32828862d3f

答案 2 :(得分:1)

*尝试一下:

class MainViewController:UIViewController{

      /*In your mainViewController create an instance(I assume the name of the 
      class, you can change this accordingly)*/

     //Create a shared instance of mainVC

   static var sharedInstance:MainViewController?

   // in viewDidLoad() assign the property to self

   override func viewDidLoad() {
    super.viewDidLoad()
     MainViewController.sharedInstance = self
} 
  func checkIfPopoverDismissed()
    {
      print("the function is triggered")
    }

}


 //Assuming the class name
class PopOverView:UIViewController{

  @IBAction func closeButton(_ sender: Any) {

  dismiss(self,animated: true, completion: 
   {MainViewController.sharedInstance?.checkIfPopoverDismissed()})

    }

答案 3 :(得分:1)

您可以使用委托来实现:

// 1. create a protocol before your PopoverViewController:
// (imagine the delegate is your helper and the protocol is the requirement of your helper)

protocol PopoverViewControllerDelegate: class {
    func popoverDidDismissed()
}

class PopoverViewController: UIViewController {
    //2. make a delegate in your view controller
    //(you need a this helper in your class to help you...)
    weak var delegate: PopoverViewControllerDelegate?


    //...

    @IBAction func closeButton(_ sender: Any) {
        //3. ask the delegate to perform its function
        //(when the time is right, ask you helper to do what he is meant to do...)
        dismiss(
            animated: true, 
            completion: delegate?.popoverDidDismissed
        )
    }
}

同时在mainViewController中...

class mainViewController: UIViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationVC = segue.destination as? PopoverViewController {
            //4. setup the delegation
            //(say that I (mainViewController) will be the helper of destinationVC)
            destinationVC.delegate = self
        }
    }
}

//5. Conform the protocol
//(need to meet the requirement as this helper, by having a function called popoverDidDismissed...)

extension mainViewController: PopoverViewControllerDelegate {
    func popoverDidDismissed() {
        // do what you want to do here in the main view controller,
        // this function will be called by PopoverViewController when the time is right
    }
}

答案 4 :(得分:1)

您需要的是放松休闲。似乎经常错过的功能。

在此处查看官方文档: https://developer.apple.com/documentation/uikit/resource_management/dismissing_a_view_controller_with_an_unwind_segue

链接页面未显示的是,您也可以将“ View Controller”图标直接拖动到“ Exit”图标,以创建手动设置,如果要先进行验证,可以从代码中调用

在上面的示例中,unwindHere在主视图控制器中定义为:

@IBAction func unwindHere(unwindSegue: UIStoryboardSegue) {
    if let sourceVC = unwindSegue.source as? PopoverViewController {
        // Do stuff
    }
}

编辑:您将在PopoverViewController中使用的操作如下所示:

@IBAction func closeButtonTapped(_ sender: UIButton) {
    performSegue(withIdentifier: "ExitPopover", sender: self)
}

在这里,“ ExitPopover”是展开搜索的标识符。