简体   繁体   中英

NSCollectionViewItem behind a NSTextField

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?

我相信您将必须继承NSTextField并重写mouseDown才能将事件传递给nextResponder(应将其设置为您的集合视图)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM