【问题标题】:How to set NotificationCenter's observer when selector is a static method选择器为静态方法时如何设置NotificationCenter的观察者
【发布时间】:2017-06-16 15:27:20
【问题描述】:
func addObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?)

此函数需要观察者是某个对象,但同时将静态方法设置为选择器。

This answer explains how to set selector and observer when the selector is an instance method.

【问题讨论】:

    标签: swift selector nsnotificationcenter observers notificationcenter


    【解决方案1】:

    我们需要将YourClass.self 设置为观察者。这样——

    NotificationCenter.default.addObserver(YourClass.self, selector: #selector(YourClass.yourStaticMethod), name: NSNotification.Name.BlahBlah, object: nil)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多