【问题标题】:How to show an alert from another class in Swift?如何在 Swift 中显示来自另一个类的警报?
【发布时间】:2016-10-13 07:47:31
【问题描述】:

我有一个主类AddFriendsController,它运行以下代码行:

ErrorReporting.showMessage("Error", msg: "Could not add student to storage.")

然后我有这个ErrorReporting.swift 文件:

导入基础

class ErrorReporting {
    func showMessage(title: String, msg: String) {
        let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

显然,self 在这里不起作用,并且给了我一个错误。我如何引用当前打开的视图控制器(即AddFriendsController 在这种情况下),因为我希望在许多不同的 swift 文件中使用相同的方法?

谢谢。

【问题讨论】:

  • 创建扩展 showMessage 。作为扩展 ErrorReporting 。然后是你的函数。
  • @sourav 你能详细解释一下吗?还在快速学习...
  • extension ErrorReporting { func showMessage(title: String, msg: String) { let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: “好的”,样式:UIAlertActionStyle.Default,处理程序:nil)) self.presentViewController(alert, animated: true, completion: nil) } }

标签: swift uiviewcontroller uialertcontroller swift2.3


【解决方案1】:

您可以为 UIApplication 创建扩展方法(例如),它将返回您的 topViewController:

extension UIApplication {

    static func topViewController(base: UIViewController? = UIApplication.sharedApplication().delegate?.window??.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return topViewController(nav.visibleViewController)
        }
        if let tab = base as? UITabBarController, selected = tab.selectedViewController {
            return topViewController(selected)
        }
        if let presented = base?.presentedViewController {
            return topViewController(presented)
        }

        return base
    }
}

然后您的课程将如下所示:

class ErrorReporting {

    static func showMessage(title: String, msg: String) {
        let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
        UIApplication.topViewController()?.presentViewController(alert, animated: true, completion: nil)
    }
}

方法需要是static 才能调用它为ErrorReporting.showMessage

【讨论】:

  • 该死的这个扩展太好了XD
  • 这是一个救生员!如此优雅!非常感谢!
【解决方案2】:

实际上,我认为视图控制器呈现操作应该在UIViewController 实例上完成,而不是在模型类中。

一个简单的解决方法是将UIViewController 实例作为参数传递

class ErrorReporting {
    func showMessage(title: String, msg: String, `on` controller: UIViewController) {
        let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
        controller.presentViewController(alert, animated: true, completion: nil)
    }
}

并像下面这样称呼它

ErrorReporting.showMessage("Error", msg: "Could not add student to storage.", on: self)

【讨论】:

    【解决方案3】:

    Maksym Musiienko 的 Swift 3 版本的答案如下:

    extension UIApplication {
    
        static func topViewController(base: UIViewController? = UIApplication.shared.delegate?.window??.rootViewController) -> UIViewController? {
    
            if let nav = base as? UINavigationController {
                return topViewController(base: nav.visibleViewController)
            }
    
            if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
                return topViewController(base: selected)
            }
    
            if let presented = base?.presentedViewController {
                return topViewController(base: presented)
            }
    
            return base
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多