首页 > 解决方案 > 使我的 ViewController 符合 NSDraggingDestination 协议

问题描述

有人可以向我解释以下代码有什么问题吗?我正在尝试使我的 ViewController 符合 NSDraggingDestination 协议,但在该行出现错误:

self.registerForDraggedTypes(supportedTypes)

告诉我“'ViewController 类型的值没有成员'registerForDraggedTypes'

这是 ViewController 代码:

import Cocoa
import Foundation


class ViewController: NSViewController, NSDraggingDestination {
    
    @IBOutlet var statusScrollView: NSScrollView!
    
    let supportedTypes: [NSPasteboard.PasteboardType] = [.tiff, .fileURL]
    

    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.registerForDraggedTypes(supportedTypes)
        
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    
    
    
    @IBAction func checkButton(_ sender: NSButtonCell) {
        print("check button pressed.")
    }
}

任何帮助深表感谢。谢谢!

标签: swiftmacosdrag-and-dropprotocolsswift5

解决方案


请检查 NSViewController 是否具有 registerForDraggedTypes(_ types:[NSPasteboard.PasteboardType]) 函数。

我认为您的 ViewController 和 NSViewController 都没有函数调用 registerForDraggedTypes()


推荐阅读