How do I disable drag and drop on NSTextView?

左心房为你撑大大i 提交于 2019-12-11 11:38:10

问题


I have a NSWindowController that contains several NSViewControllers. I would like to universally accept drag and drop events with the NSWindowController class and not be intercepted by other views such as NSTextView (contained in a NSViewController)

How can I tell NSTextView to ignore the drag & drop event?


回答1:


I found out that there were two things needed to skip past NSTextView's interception of the drag and drop event.

In the NSViewController containing your NSTextView:

- (void)awakeFromNib
{
    [self noDragInView:self.view];
}

- (void)noDragInView:(NSView *)view
{
    for (NSView *subview in view.subviews)
    {
        [subview unregisterDraggedTypes];
        if (subview.subviews.count) [self noDragInView:subview];
    }
}

Now subclass your NSTextView and add this method:

- (NSArray *)acceptableDragTypes
{
    return nil;
}

The NSTextView should now properly ignore the drag and drop event and leave it to be handled by the NSWindow.




回答2:


It is sufficient to subclass the NSTextView and override the getter for its acceptableDragTypes property, no need to unregisterDraggedTypes. In Swift:

override var acceptableDragTypes : [String] {
    return [String]()
}



回答3:


Slight update.

import Cocoa

class MyTextView : NSTextView {
    // don't accept any drag types into the text view
    override var acceptableDragTypes : [NSPasteboard.PasteboardType] {
        return [NSPasteboard.PasteboardType]()
    }
}



回答4:


Swift 5

import Cocoa

class NSTextViewNoDrop: NSTextView {

    override var acceptableDragTypes: [NSPasteboard.PasteboardType] { return [] }

}


来源:https://stackoverflow.com/questions/16482596/how-do-i-disable-drag-and-drop-on-nstextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!