0

我正在尝试覆盖我的自定义 ViewControllers 的 viewDidAppear() 方法,并且我找到了使用 swizzle 方法的解决方案。它可以工作,但是当键盘可见时,我的方法也会收集键盘 UIViewControllers 之类的;

    UIInputWindowController
    UICompatibilityInputViewController
    UISystemKeyboardDockController
    UIInputWindowController
    UICompatibilityInputViewController
    UICompatibilityInputViewController
    UISystemInputAssistantViewController
    UIPredictionViewController
    UISystemKeyboardDockController

我想知道如何排除除自定义 ViewControllers 之外的所有 UIViewControllers?我可以排除我目前遇到的那些 UIViewControllers,但我认为将来还会有 UIViewControllers。我想我需要一个通用的解决方案。

我的代码如下:

extension UIViewController {
    @objc func viewDidApperOverride(_ animated: Bool) {
        self.viewDidApperOverride(animated)
        print(NSStringFromClass(type(of: self)))  
    }

    static func swizzle() {
        if self != UIViewController.self {
            return
        }
        
        let originalSelector = #selector(UIViewController.viewDidAppear(_:))
        let swizzledSelector = #selector(UIViewController.viewDidApperOverride(_:))
        guard let originalMethod = class_getInstanceMethod(self, originalSelector),
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) else { return }
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
}
4

0 回答 0