7

基于视图的 NSTableView 似乎只是标准行为,其中,为了使表格内的文本字段成为第一响应者,用户必须双击或单击并“保持冷静”。

然而,鉴于基于视图的 NSTableViews 提供的灵活性,这种行为并不总是可取的,因为现在有许多不同和复杂的应用程序可能不仅仅是做一个“老派”表。

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

4

1 回答 1

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

并立即返回 YES,以便快速做出 firstResponder。如果文本字段被击中,表格会“延迟”做出第一响应者,并且除非首先选择该行,否则不允许做出它。

于 2012-06-25T17:00:05.913 回答