将自定义NSView添加到NSScrollView时的通知

时间:2017-06-14 08:14:02

标签: nsview nsscrollview

关于这个问题(Custom NSView embedded in NSSscrollView),当我的自定义视图使用IB嵌入到NSScrollView中时,我可以在awakeFromNib方法中检测到这一点并执行所需的初始化(我必须设置一些滚动通知)。登记/> 我的问题是当自定义视图从代码添加到NSScrollView时,设置scrollview的documentView属性。在这种情况下我的自定义视图以任何方式通知(一些被调用的NSView方法,我可以覆盖)或者我必须在将视图添加到滚动视图后显式执行我的初始化?

1 个答案:

答案 0 :(得分:1)

The documentView of the scroll view is a subview of this scroll view and NSView has lots of usefull methods. In the chapter "Managing the View Hierarchy":

viewDidMoveToSuperview

Informs the view that its superview has changed (possibly to nil).

viewDidMoveToWindow

Informs the view that it has been added to a new view hierarchy.

When the view is added to a superview, viewDidMoveToSuperview is called. But the view could already be inside a view (in a XIB or in code) and this superview could be added to a scroll view. viewDidMoveToWindow is called when the view has been added to or removed from a view hierarchy.