ios – 从协议扩展中调用选择器

前端之家收集整理的这篇文章主要介绍了ios – 从协议扩展中调用选择器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建简单的主题引擎,并希望有一个扩展,将UISwipeGestureRecognizer添加到UIViewController

这是我的代码

protocol Themeable {
    func themeDidUpdate(currentTheme: Theme) -> Void
}

extension Themeable where Self: UIViewController {
    func switchCurrentTheme() {
        Theme.switchTheme()
        themeDidUpdate(Theme.currentTheme)
    }

    func addSwitchThemeGestureRecognizer() {
        let gestureRecognizer = UISwipeGestureRecognizer(target: self,action:#selector(Self.switchCurrentTheme))
        gestureRecognizer.direction = .Down
        gestureRecognizer.numberOfTouchesrequired = 2
        self.view.addGestureRecognizer(gestureRecognizer)
    }
}

当然编译器找不到#selector(Self.switchCurrentTheme),因为它没有通过@objc指令公开.是否可以将此行为添加到我的扩展程序?

更新:主题是一个Swift枚举,所以我不能在Themeable协议前添加@objc

解决方法

我能想到的最干净,最有效的解决方案是使用相关方法在UIViewController上定义一个私有扩展.通过将范围限制为私有,对此方法的访问被隔离到定义协议的源文件中.这是它的样子:
protocol Themeable {
    func themeDidUpdate(currentTheme: Theme) -> Void
}

fileprivate extension UIViewController {
    @objc func switchCurrentTheme() {
        guard let themeableSelf = self as? Themeable else {
            return
        }

        Theme.switchTheme()
        themeableSelf.themeDidUpdate(Theme.currentTheme)
    }
}

extension Themeable where Self: UIViewController {
    func addSwitchThemeGestureRecognizer() {
        let gestureRecognizer = UISwipeGestureRecognizer(target: self,action:#selector(switchCurrentTheme))
        gestureRecognizer.direction = .Down
        gestureRecognizer.numberOfTouchesrequired = 2
        self.view.addGestureRecognizer(gestureRecognizer)
    }
}
原文链接:https://www.f2er.com/iOS/334954.html

猜你在找的iOS相关文章