Can we use a class (.self) as the target in addTarget(_:action:for:) for static event handlers?

I am building a centralized event handling system for UIKit controls and gesture recognizers. My current approach registers events using static methods inside a handler class, like this:

internal class TWOSInternalCommonEventKerneliOS {

    internal static func RegisterTouchUpInside(_ pWidget: UIControl) -> Void {
        pWidget.addTarget(
            TWOSInternalCommonEventKerneliOS.self,
            action: #selector(TWOSInternalCommonEventKerneliOS.WidgetTouchUpInsideListener(_:)),
            for: .touchUpInside
        )
    }

    @objc
    internal static func WidgetTouchUpInsideListener(_ pWidget: UIView) -> Void {
        print("WidgetTouchUpInside")
    }
}

This works in my testing because the methods are marked @objc and static, but I couldn’t find Apple documentation explicitly confirming whether using ClassName.self (instead of an object instance) is officially supported.

Questions:

Is this approach (passing ClassName.self as the target) recommended or officially supported by UIKit?

If not, what is the safer alternative to achieve a similar pattern, where event registration can remain in static methods but still follow UIKit conventions?

Would using a shared singleton instance as the target (e.g., TWOSInternalCommonEventKerneliOS.shared) be the correct approach, or is there a better pattern?

Looking for official guidance to avoid undefined behavior in production.

Answered by Frameworks Engineer in 855558022

Its an unusual pattern, but there is no reason why it shouldn't work - calling a class method is fundamentally the same as calling an instance method, with self just being the class instead of an instance.

Accepted Answer

Its an unusual pattern, but there is no reason why it shouldn't work - calling a class method is fundamentally the same as calling an instance method, with self just being the class instead of an instance.

Can we use a class (.self) as the target in addTarget(_:action:for:) for static event handlers?
 
 
Q