简体   繁体   中英

How do I enable drag drop of a file onto a NSTextField?

How do I enable drag drop of a file onto a NSTextField ?

I thought when I drop a file from filesystem to a NSTextField in my app, the path to the file is automatically written in the field.

Instead I can't drop it, and the field remains empty.

I've added this line:

[myTextField registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];

but it doesn't work.

I couldn't find any drag&drop related method in the delegates: NSControlTextEditingDelegate or NSTextFieldDelegate .

Thanks

You will need to subclass the NSTextField and override the -performDragOperation: method. Apple has several examples of how to handle dropped files in their Drag and Drop Programming Topics .

In case you set the NSTextFields behavior to editable you can drop a file right away without a line of code. Xcode 11, but also worked i earlier versions.

在此处输入图片说明

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