首页 > 解决方案 > 如何通过swift中的函数调用打开弹出窗口(单独的视图控制器)

问题描述

新的 swift 用户在这里。我想在普通视图控制器上打开一个弹出窗口(由单独的视图控制器控制)作为函数调用的一部分(即没有按下或类似的按钮)。我怎么写这个?我还想发送信息来影响弹出窗口中显示的图像。

我以前设法通过按下按钮打开一个模式弹出窗口。我真的不知道这些弹出窗口是否有什么特别之处,但如果有的话,我想让上面的弹出窗口看起来一样。

我希望这足以让某人了解我的需要。

标签: swiftpopup

解决方案


您可以像这样在超级视图控制器中添加一个方法:

class MainViewController: UIViewController {

    // MARK: - View methods
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    /// This Method is used for Alert With Action Method
    func showAlertViewWithPopAction(message: String, title: String) {

        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        let okAction = UIAlertAction(title: "OK", style: .default) { [weak self] _ in
                ///add you logic here
         }
         alert.addAction(okAction)
         self.present(alert, animated: true, completion: nil)
      }
}

并像这样在每个控制器中继承此控制器,并从控制器中调用此方法,如下所示:

 class FirstViewController: MainViewController {

        // MARK: - View methods
        override func viewDidLoad() {
            super.viewDidLoad()
        ///Call alert method according to your use
            Self.showAlertViewWithPopAction(message: "This is demo alert", title: "Alert")

        }
   }

推荐阅读