问题
I have a NSTextField
on the view of an NSCollectionViewItem
.
When you click an NSCollectionViewItem
the item is selected.
When you click the NSTextField
it gains focus, but the NSCollectionViewItem
which is behind the text field does not get selected.
I want to change this last behaviour so the text field gets focus and the view item also gets selected.
What's the best way to do this?
回答1:
I believe you would have to subclass NSTextField and override mouseDown to pass the event to the nextResponder (which should be set to your collection view)
回答2:
I also have an NSCollectionViewItem that contains an NSTextField. I'm trying to recreate the basic look and feel of the text label of an Icon in the Finder. I haven't finished it yet, but what I have so far seems to answer this question.
The key is to have the text field start out as NOT selectable. A mouse click on the text field will be ignored by the text field and will select the collection view item. Then in the collection view item's setSelection method, when it is being selected, set the text field as editable. The next mouse click on the text field will give it focus. Then when the collection view item is being unselected, set the text field back to not selectable and wait for the collection view item to be selected again.
-(void)awakeFromNib
{
[self.textField setSelectable:NO];
}
-(void)setSelected:(BOOL)selected
{
[super setSelected:selected];
if (self.selected)
{
[self.textField setEditable:YES];
}
else
{
[self.textField setSelectable:NO];
}
}
This answers the above question, but obviously, more is needed for a complete solution.
来源:https://stackoverflow.com/questions/9436259/nscollectionviewitem-behind-a-nstextfield