I'm creating multiple different UI subclasses which can be created using JSContext
, and I'd like them all to inherit a certain set of methods.
The classes vary from NSButton
to NSTextView
. It would be nice to have a single subclass act as NSView
parent class, but I can't figure out if this is possible — or how to begin searching.
For example:
@interface CustomButtonClass : NSButton
I'm looking for a way to make NSButton
inherit from a custom NSView
subclass.
Is there a way to go around this, or do I need to create multiple intermediate classes?
CodePudding user response:
The answer was super obvious, but coming from different languages, I struggled to make the connection. Thanks to all the comments, it was super easy to figure out.
Rather than trying to insert an intermediate class somewhere in the inheritance tree, you can create a category for NSView
. Categories can also include all the required protocols.
For example, in my case, I wanted to expose multiple NSView
methods to JSContext
, and create some of my own, which would work on any NSView
-derived object.
@protocol JSInterfaceExports <JSExport>
// Anything you want to expose to JS
- (void)customMethod;
// You can also expose all the common NSView properties
// and methods to JS in this protocol
@property (nonatomic) NSRect frame;
- (void)removeFromSuperview;
@end
@interface NSView (JSInterface)
// Any custom methods and properties
- (void)customMethod;
@end
Include this file on any NSView
subclass, make them comply to <JSInterfaceExports>
, and you are set.