Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop to NSArrayController

Tags:

macos

swift

I am struggling to pass a variable received from a drag and drop (file from finder onto NSView) to a NSArrayController in AppDelegate.

NSView receives the drag and drop:

class MyView:NSView, NSDraggingDestination {

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
    }

    let fileTypes = ["jpg", "jpeg", "bmp", "tif", "TIF"]
    var fileTypeIsOk = false
    var droppedFilePath = ""


    required init?(coder: NSCoder) {
        let types = [NSFilenamesPboardType, NSURLPboardType, NSPasteboardTypeTIFF]
        super.init(coder: coder)
        registerForDraggedTypes(types)
    }
    override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation {

        if checkExtension(sender) == true {
            fileTypeIsOk = true
            return .Every
        } else {
            fileTypeIsOk = false
            println("Dropped images rejected.")
            return .None
        }
    }
    override func performDragOperation(sender: NSDraggingInfo) -> Bool {

        var go = false
        if let board =  sender.draggingPasteboard().propertyListForType("NSFilenamesPboardType") as? NSArray {            
            for indexFile in 0...board.count-1 {
                println("Files dropped \(board[indexFile])")
                //-> pass the variable board[indexFile] or board[] to NSArrayController

                go = true
            }
        }
        return go
    }

    func checkExtension(drag: NSDraggingInfo) -> Bool {
        var go = false
        var foundExtension = 0
        var numberOfFiles = 0

        if let board = drag.draggingPasteboard().propertyListForType("NSFilenamesPboardType") as? NSArray {
            numberOfFiles = board.count
            for indexFile in 0...numberOfFiles-1 {
                if let url = NSURL(fileURLWithPath: (board[indexFile] as! String)) {
                    let suffix = url.pathExtension!
                    for ext in fileTypes {
                        if ext.lowercaseString == suffix {
                            ++foundExtension
                            break
                        }
                    }
                }
            }
        }
        if foundExtension == numberOfFiles {
            println("Dropped files have valid extension.")
            go = true
        } else {
            println("At least one file dropped has an invalid extension.")
        }
        return go
    }
}

in AppDelegate, I have set up an NSArrayController (pointing to data[] at the moment but I don't know how to populate data[] from board[])

func numberOfRowsInTableView(aTableView: NSTableView) -> Int {
    return data.count
}

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {

    var cellView: NSTableCellView = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! NSTableCellView

    println("Reload Array")

    if tableColumn!.identifier == "fileNameColumn" {
        cellView.imageView!.image = NSImage(named: "GreenCircle")
        cellView.textField!.stringValue = data[row]

        return cellView
    }

...

thank you for your help

like image 408
Francois in UK Avatar asked Nov 27 '25 16:11

Francois in UK


1 Answers

Call the "- (void)addObjects:(NSArray *)objects;" method on your NSArrayController and pass it board:

[self.myArrayController addObjects:board];
like image 51
geowar Avatar answered Nov 29 '25 07:11

geowar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!