如何获取所有UIViewControllers ViewWillAppear的回调

时间:2017-06-21 07:19:03

标签: ios swift

当屏幕上出现UIViewController时,我正在使用调配来调用方法。

代码:

extension UIViewController {

    open override class func initialize() {

        guard self === UIViewController.self else { return }

        let originalSelector = #selector(self.viewWillAppear(_:))
        let swizzledSelector = #selector(self.proj_viewWillAppear(animated:))
        let originalMethod = class_getInstanceMethod(self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }

    func proj_viewWillAppear(animated: Bool) {
        self.proj_viewWillAppear(animated: animated)
        let viewControllerName = NSStringFromClass(type(of: self))
        print("viewWillAppear: \(viewControllerName)")
    }
}

但我收到了警告:

  

方法'initialize()定义了Objective-C类方法'initialize',它不能保证被Swift调用,并且在将来的版本中将被禁止

是否有不同的方法来获得相同的结果?

谢谢

0 个答案:

没有答案