【问题标题】:Making my ViewController conform to NSDraggingDestination protocol使我的 ViewController 符合 NSDraggingDestination 协议
【发布时间】:2020-06-27 15:23:47
【问题描述】:

有人可以向我解释以下代码有什么问题吗? 我正在尝试使我的 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.")
    }
}

非常感谢任何帮助。 谢谢!

【问题讨论】:

标签: swift macos drag-and-drop protocols swift5


【解决方案1】:

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

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

【讨论】:

  • 显然有很多我不明白,但是我认为 registerForDraggedTypes(_ :) 是一个函数,它是 NSDraggingDestination 协议的一部分。
  • 你必须将NSDraggingDestination协议的函数名称写入你的ViewController,然后你才能在你的ViewController中调用它。
  • 啊……对。我知道了。感谢您打开我脑海中昏暗灯光的开关。 :-)
【解决方案2】:

registerForDraggedTypes(_:)NSView(和NSWindow)的方法。打电话

self.view.registerForDraggedTypes(supportedTypes)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多