问题
How would you Subclass an NSOutlineView?
回答1:
Firstly, before you subclass an object, you should ask yourself "do I need to do this?". Subclassing is a more complex way of interacting and extending the functionality of other classes, so you should make sure that what you are trying to achieve cannot easily be achieved through another, simpler method, such as delegation, notifications etc. Obviously if you are trying to change the way the control looks this is going to be more easily accomplished through subclassing, but make sure you check all the other available options. Remember that NSOutlineView
has a fairly long object tree - it inherits from NSTableView
, which inherits from NSControl
, which inherits from NSView
, which inherits from NSResponder
which inherits from NSObject
, and there are various helper methods that exist in each of these classes which can often help you achieve what you want.
However, if you check all of these options and decide to subclass NSOutlineView
, it depends what you want to do with your subclass. The easiest way to create the shell of your subclass would be to select File > New File
then choosing Objective-C class
, as you would with any other class, which will create a new class, with header and implementation files, that inherits from NSObject
. Then you can simply change the line in your header file:
@interface MyClass : NSObject { // Where MyClass is the name of your class
to
@interface MyClass : NSOutlineView {
which will cause your class to inherit from NSOutlineView
. Since it is a subclass of NSOutlineView
, this gives you lots of opportunities to change the default behaviour of the control.
Since you are creating a subclass, you can change the default implementation of any method up the object tree - that is, you can override methods declared in NSOutlineView
, NSTableView
, NSControl
, NSView
, NSResponder
and NSObject
(although you should rarely override methods declared in NSObject
). You do not need to redefine the method signature in your header file, you can simply override the function by implementing it in your subclass's implementation. For example, if you wanted to override NSView
's drawRect:
method, you would do the following in your subclass's implementation:
- (void)drawRect:(NSRect)rect //Method signature from the docs
{
//Code here
}
When drawRect:
is called upon your class, your code will be executed instead of the code in NSView
.
You can also pass method calls up the tree for methods that you do not want to handle. This is done by default, so you do not need to create empty methods that simply call the method on super
, however, if you override a method and want to allow one of your superclasses to handle it first, you could do the following:
- (void)expandItem:(id)item
{
[super expandItem:item];
//Your code here
}
This would be beneficial if you wanted to change a variable in your class, for example, but provide the default implementation of the method by first passing the method call up the tree.
Subclassing can be a rather complex process, especially for such a complex object as a control, although it can be very useful and powerful.
回答2:
@interface MyOutlineView : NSOutlineView
{
...
}
...
@end
来源:https://stackoverflow.com/questions/992389/subclassing-nsoutlineview