子类化NSOutlineView [英] Subclassing NSOutlineView

查看:140
本文介绍了子类化NSOutlineView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何子类NSOutlineView?

How would you Subclass an NSOutlineView?

推荐答案

需要这样做吗?子类化是一种更复杂的交互和扩展其他类的功能的方式,所以你应该确保你想要实现的东西不能通过另一个更简单的方法来实现,比如委托,通知等。显然,如果你试图改变控件的外观,这将是通过子类化更容易实现,但确保你检查所有其他可用的选项。记住, NSOutlineView 有一个相当长的对象树 - 它继承自 NSTableView ,它继承自 NSControl ,继承自 NSView ,继承自 NSResponder $ c> NSObject ,并且在每个类中都有各种帮助方法,通常可以帮助你实现你想要的。

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.

但是,如果你检查所有这些选项,并决定子类 NSOutlineView ,它取决于你想做什么与你的子类。创建子类的shell的最简单的方法是选择 File>新文件然后选择 Objective-C类,就像你将与任何其他类一样,将创建一个新类,包含头文件和实现文件,继承自 NSObject 。然后你可以简单地改变头文件中的行:

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

@interface MyClass : NSOutlineView {

这将导致你的类继承 NSOutlineView 。因为它是 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.

因为你正在创建一个子类,你可以改变对象树上任何方法的默认实现 - 也就是说,你可以覆盖 NSOutlineView NSTableView NSControl NSView NSResponder NSObject (尽管你应该很少覆盖 NSObject 中声明的方法。您不需要在头文件中重新定义方法签名,您可以通过在子类的实现中实现它来简单地覆盖函数。例如,如果要覆盖 NSView drawRect:方法,则在子类的实现中执行以下操作:

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
}

drawRect:在您的类中调用,您的代码将被执行,而不是 NSView 中的代码。

When drawRect: is called upon your class, your code will be executed instead of the code in NSView.

你也可以在你不想处理的方法的树上传递方法。这是默认完成的,所以你不需要创建空方法,只需调用 super 的方法,但是,如果你重写一个方法,并且想允许你的一个超类来处理它,你可以做以下:

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.

这篇关于子类化NSOutlineView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆