【问题标题】:Common protocol for UIViewcontroller and UITableviewcontroller SwiftUIViewcontroller 和 UITableviewcontroller Swift 的通用协议
【发布时间】:2019-08-31 14:03:50
【问题描述】:

我正在编写一些常用方法来显示警报加载器之类的东西,

我希望从 uiviewcontroller 和 uitableviewcontroller 子类访问这些方法,

我们怎样才能达到同样的效果?

【问题讨论】:

    标签: ios swift uitableview uiviewcontroller


    【解决方案1】:

    创建UIViewController 扩展并添加您的警报方法,如下所示。

    extension UIViewController {
    
        func showAlert(title: String, message: String, buttonName: String, alertActionHandler: ((UIAlertAction) -> Void)? = nil) {
    
            guard let alertActionHandler = alertActionHandler else {
                return
            }
    
            let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
            let actionButton = UIAlertAction(title: buttonName, style: .default, handler: { action in
                alertController.dismiss(animated: true, completion: nil)
                alertActionHandler(action)
            })
            alertController.addAction(actionButton)
            self.present(alertController, animated: true, completion: nil)
        }
    }
    

    您可以在视图控制器的任何位置调用showAlert 方法。

    【讨论】:

      【解决方案2】:

      UITableViewControllerUIViewController 的子类,您可以创建UIViewController 的扩展,并且其中声明的函数可以被两者的实例访问:

      extension UIViewController {
          func showAlert(title: String, message: String) {
              ...
          }
      }
      

      【讨论】:

        【解决方案3】:

        创建 NSObject 类型的通用文件,如下代码。然后您可以从UIViewControllerUITableviewController 或任何文件/控制器调用。

        import UIKit
        
        class AppUtils: NSObject {
        
            static func showAlert(title: String, message: String) {
                DispatchQueue.main.async {
                    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
                    let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
                    alert.addAction(ok)
                    UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
                }
            }
        }
        

        调用showAlert函数:-

        AppUtils.showAlert(title: "My Title", message: "My Message")
        

        【讨论】:

        • 为什么AppUtils 需要是NSObject 的子类?这完全没有必要……
        • 嗨@devidPasztor,如果你创建 UIViewController 的扩展,那么你不能像从 UIView 类一样调用任何地方,如果你使用任何 XIB 和 UIView 那么你不能从 UIView 类调用这个方法,但是如果你从 NSObject 创建 AppUtil 类,然后你可以在任何地方使用这个函数,所以这是解决这个问题的最好方法。
        • OP 想要从所有 视图控制器 调用此方法,因此无需通过 UIView 提供它。此外,让你的类从NSObject 继承并不会改变它的可见性,你可以在没有继承的情况下实现同样的效果。
        • 好的,感谢您提供的信息,但是如果我们想从 UIview 或 UIViewController 或 UITableviewController 调用像 showAlert 这样的常用函数,那么我们需要做什么?我们如何解决这个问题?,我不想创建多个通用函数,请建议我。
        • @AtulParmar 查看我的解决方案
        【解决方案4】:

        你可以这样写一个全局方法:

        func showAlert(_ view: UIView, title: String, message: String) {
            DispatchQueue.main.async {
                let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
                    switch action.style{
                        case .default:
                            print("default")
        
                        case .cancel:
                            print("cancel")
        
                        case .destructive:
                            print("destructive")
        
                    }}))
        
                view.present(alert, animated: true, completion: nil)
            }
        }
        

        并在每个 ViewController 中使用它:

         showAlert(self.view, title: Alert, message: "Hi")
        

        【讨论】:

        • 如果你想让一个方法可以被所有的视图控制器访问,你可以简单地扩展UIViewController,不需要声明一个全局函数。
        猜你喜欢
        • 2018-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-03
        • 1970-01-01
        相关资源
        最近更新 更多