您好,我有一个实用程序类,其中我像这样声明了 AlertViewFunction
func displayAlertMessage(userMessage: String,//controller){
let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated: true, completion: nil)
}
问题是我不能在这里使用self
self.presentViewController(myAlert, animated: true, completion: nil)
我想将 Controller 传递给这个函数,这样我就可以像这样使用
controller.presentViewController(myAlert, animated: true, completion: nil)
如何从任何 ViewController 传递 Controller 。假设我在 LoginViewController
Utility().displayAlertMessage(Message.INTERNETISNOTCONNECTED,//controller)
最佳答案
Utility().displayAlertMessage(Message.INTERNETISNOTCONNECTED, controller: self)
和
func displayAlertMessage(userMessage: String, controller: UIViewController)
{
controller?.presentViewController(myAlert, animated: true, completion: nil)
}
关于ios - 如何将 Controller 传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35710214/