来自其他swift.file的swift3调用alert函数

Cal*_*han 2 ios swift3

我是swift3的新手.现在,我正在寻找一种从其他swift.file调用alert函数的方法

像这样:

//MainView.swift
//Call function
AlertFun.ShowAlert(title: "Title", message: "message..." )

//Another page for storing functions
//Function.swift

public class AlertFun {
    class func ShowAlert(title: String, message: String ) {    
        let alert = UIAlertController(title: tile, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}
Run Code Online (Sandbox Code Playgroud)

问题在这里......不能这样做......

    self.present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

我该如何实现它?谢谢.

roc*_*ift 5

将viewController引用作为参数传递给showAlert函数,如:

//MainView.swift
//Call function
AlertFun.ShowAlert(title: "Title", message: "message...", in: self)

//Another page for storing functions
//Function.swift

public class AlertFun {
    class func ShowAlert(title: String, message: String, in vc: UIViewController) {    
        let alert = UIAlertController(title: tile, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
        vc.present(alert, animated: true, completion: nil)
    }
}
Run Code Online (Sandbox Code Playgroud)