ios - 如何将 Controller 传递给函数

标签 ios swift uiviewcontroller

您好,我有一个实用程序类,其中我像这样声明了 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/

相关文章:

ios - 使用类似 "tag"的单元格设置 collectionView

ios - Swift 中的 SHA256 - 导入框架问题

swift - 使用 `__FUNCTION__` 作为 `objc_getAssociatedObject` 的 key 是否可以

ios - 使用容器自动布局

ios - 初始化器不会覆盖其父类(super class)中的指定初始化器

ios - 如何在 Swift 中将一个 viewController 的 View 加载到另一个 viewController 的 View ?

ios - 使用swift ios将accessToken发送到服务器

ios - 如何在 Nativescript 中有效禁用暗模式主题?

ios - 如何在 ios 中制作自定义 UILabel

swift - SKLabelNode 的奇怪定位