在基于视图的NSTableView中,如何通过单击使控件成为第一个响应者?

C. *_*ith 7 macos cocoa objective-c nstableview nscontrol

基于视图的NSTableViews似乎只是标准的行为,其中,为了使文本字段的表里面的第一个响应者,用户要么双击或单点击"保持冷静".

但是,考虑到基于视图的灵活性NSTableViews提供,这种行为并不总是令人满意,因为现在有许多不同的复杂应用程序可能而不仅仅是做一个"旧学校"表.

如何通过单击在基于视图的NSTableView中轻松地控制(可能在单元格中与其他控件一起)第一响应者?

cor*_*unn 21

要解决此问题,请在NSTableView上覆盖此方法:

@interface NSResponder (NSControlEditingSupport)

/* This is a responder chain method to allow controls to determine when they should become first responder or not. Some controls, such as NSTextField, should only become first responder when the enclosing NSTableView/NSBrowser indicates that the view can begin editing. It is up to the particular control that wants to be validated to call this method in its -mouseDown: (or other time) to determine if it should attempt to become the first responder or not. The default implementation returns YES when there is no -nextResponder, otherwise, it is forwarded up the responder chain. NSTableView/NSBrowser implements this to only allow first responder status if the responder is a view in a selected row. It also delays the first responder assignment if a doubleAction needs to (possibly) be sent. 'event' may be nil if there is no applicable event.
*/
- (BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event NS_AVAILABLE_MAC(10_7);

@end
Run Code Online (Sandbox Code Playgroud)

并立即返回YES以允许快速制作第一个响应者.如果文本字段被命中,该表"延迟"制作第一响应者,并且除非首先选择该行,否则不允许它进行.