如何将参数传递给可在该函数内使用的选择器

时间:2017-02-09 07:36:39

标签: uiviewcontroller swift3 selector nsnotificationcenter nsnotifications

这里我想使用我传递UIViewController的类函数checkInternet,这样即使我在任何类中使用此类函数,它应该更改将显示互联网连接的背景颜色,所以我收到错误我无法将该参数传递给另一个可以在函数内部使用的函数,并且该UIViewController的背景将被更改。

import UIKit

    class ViewController: UIViewController {

      override func viewDidLoad() {
        super.viewDidLoad()
        NetworkConnection.checkInternet(sender:self)
      }


      override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
      }


    }

    -----------------------------------------------------------

        import UIKit
        import Foundation
        import SystemConfiguration

        class NetworkConnection: UIViewController {

          class func checkInternet(sender:UIViewController){
            do {
              reachability = try Reachability()
              NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(_:notification:)), name: .reachabilityChanged, object: reachability)
              do {
                try reachability?.startNotifier()
              } catch let error as ReachabilityError {
                print(error.localizedDescription)
              } catch {
                print(error.localizedDescription)
              }

            } catch {

            }

          }

          func reachabilityChanged(_ sender:UIViewController ,notification: NSNotification ) {
        guard let reachability = notification.object as? Reachability else { return }
        switch reachability.networkStatus {
        case .notReachable:
          sender.backgroundColor = .red
        case .reachableViaWiFi:
          sender.backgroundColor = .green
        case .reachableViaWWAN:
          sender.backgroundColor = .yellow
        }
        print("NetworkStatus: ",reachability.networkStatus)
        print("Reachable: ", reachability.reachable)
        print("Reachable Via WiFi: ", reachability.reachableViaWiFi)
      }
    }

0 个答案:

没有答案