问题描述
我有一个自定义控件.如果它继承自 NSView
,当我单击它时,它将自动成为第一响应者.如果它是从 NSControl
继承的,则不是.即使我重写 mouseDown(with:)
并且不调用super,行为上的差异仍然存在.
I have a custom control. If it inherits from NSView
, it automatically becomes the first responder when I click on it. If it inherits from NSControl
, it does not. This difference in behavior persists, even if I override mouseDown(with:)
and don't call super.
代码:
class MyControl: NSView {
override var canBecomeKeyView: Bool { return true }
override var acceptsFirstResponder: Bool { return true }
override func drawFocusRingMask() { bounds.fill() }
override var focusRingMaskBounds: NSRect { return bounds }
override func draw(_ dirtyRect: NSRect) {
NSColor.white.set()
bounds.fill()
}
}
如您所见,我覆盖了与关键视图和响应者相关的其他方法和属性中的 acceptsFirstResponder
.我还检查了 refusesFirstResponder
属性.设置为false.
As you can see, I override acceptsFirstResponder
among other methods and properties that are key view and responder related. I have also checked the refusesFirstResponder
property. It is set to false.
- 这种行为差异的原因是什么?
- 是否存在可以重写以影响它的方法或属性?
- 说我想要一种行为,即单击该视图时该视图成为第一响应者,并且该视图从
NSControl
继承,在开始时调用window!.makeFirstResponder(self)
我的鼠标按下事件处理程序是一个好的解决方案,还是有更好的解决方案?
- What is the reason for this difference in behavior?
- Is there a method or property that I can override to influence it?
- Say I want the behavior where the view becomes the first responder when clicked and the view inherits from
NSControl
, is callingwindow!.makeFirstResponder(self)
at the beginning of my mouse-down event handler a good solution or is there a better one?
推荐答案
要覆盖的属性是 needsPanelToBecomeKey
.
此属性的默认值为false.子类可以重写此属性,并使用其实现来确定视图是否要求其面板成为键窗口,以便它可以处理键盘输入和导航.这样的子类还应该重写acceptsFirstResponder以返回true.
The default value of this property is false. Subclasses can override this property and use their implementation to determine if the view requires its panel to become the key window so that it can handle keyboard input and navigation. Such a subclass should also override acceptsFirstResponder to return true.
此属性还用于键盘导航.它确定鼠标单击是否应将焦点集中在视图上(即,使其成为第一响应者).某些视图(例如,文本字段)在单击时希望获得键盘焦点.其他视图(例如,按钮)仅在您按Tab键时才获得焦点.您不希望仅仅因为单击了复选框就将焦点从正在进行编辑的文本字段转移到了
This property is also used in keyboard navigation. It determines if a mouse click should give focus to a view—that is, make it the first responder). Some views (for example, text fields) want to receive the keyboard focus when you click in them. Other views (for example, buttons) receive focus only when you tab to them. You wouldn't want focus to shift from a textfield that has editing in progress simply because you clicked on a check box.
NSView
返回 true
, NSControl
返回 false
.
这篇关于鼠标按下行为NSControl和NSView的第一响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!