Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Hey all. I've been trying to understand drag-and-drop for the longest time, but I still can't get it. What I'm trying to do is create an NSTableView that keeps track of files. For example, a user can drag an .rtf text file into the tableview, and it will simply maintain a link to the file. (Probably using an NSArrayController of some kind.) Problem is, I can't get it to even accept any type of dragged files. TIA! :)
 

cazlar

macrumors 6502
Oct 2, 2003
492
11
Sydney, Australia
Someone can probably answer this better, but IIRC you need to register which types your tableview will accept. I think you want NSFilenamesPboardType to accept filenames from Finder?

Then you need to have the methods to validate the drop, and accept it. Somewhere in there you will probably want to convert the NSData containing the filenames into an array of strings which you can then use.

Hope this helps. I've only done it once so I'm no expert!
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I've attached an example showing one way to get this working. I think there may be a simpler way using NSTableView delegates and a 10.4 only call to force the table view to allow inter-app drags but this is the way I know!
 

Attachments

  • FinderDragDropExample.zip
    40.4 KB · Views: 969

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
I've attached an example showing one way to get this working. I think there may be a simpler way using NSTableView delegates and a 10.4 only call to force the table view to allow inter-app drags but this is the way I know!
Ah thank you so much, your example made sense :) One quick question now, is there a way to alter to "action" of double-clicking on an item in the list? Currently it begins editing the name (path), but I was curious if there's a way to set it to do a "Show in Finder" type action. Thanks again :D
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
You can use the -[NSTableView setDoubleAction:] method to get your controller to do whatever you want in response to a double-click. Make sure you also set the target if it isn't already set in Interface Builder.

You should also use Interface Builder to disable editing for your table columns.
 

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
You can use the -[NSTableView setDoubleAction:] method to get your controller to do whatever you want in response to a double-click. Make sure you also set the target if it isn't already set in Interface Builder.

You should also use Interface Builder to disable editing for your table columns.
Thanks for the quick reply, I actually just found this same answer on Cocoabuilder's forums :D
 

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Alright... I think I have one last, simple question. How can I (this is going to sound so easy it'll blow your mind...) tell the Finder to open an application? I've been looking through NSApplication and the method application:eek:penFile:, but I think that's just a delegate to be used when documents are dragged onto the dock icon. I have a string representation of the file, and have correctly implemented my table and the double-click methods, I just need to tell Finder to open the file (with the default app). Seems easy enough, but I can't find a solution. Thanks! :)
 

HexMonkey

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
The following will open the file specified by the string filePath in the default application for that file type:
Code:
[[NSWorkspace sharedWorkspace] openFile:filePath];
 

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
The following will open the file specified by the string filePath in the default application for that file type:
Code:
[[NSWorkspace sharedWorkspace] openFile:filePath];
Great :D I had actually gotten it working using an NSTask set to /usr/bin/open but your way's much easier. ;)

Problem is, though the storing of files works completely, along with my Content Set bindings and whatever else, the size of my NSArrayController's selectedObjects always comes back as 0. I've got everything connected, the values are stored, the double-click methods work, and the code/bindings are identical to all my other controllers of this type. Is subclassing NSTableview causing problems with the connection to the NSArrayController? I'm just really confused. :confused:

I know this is somewhat of a blind question to answer, but if you could think of any suggestions that would cause this type of situation to not work, I'd really appreciate it. :D
 

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Gah... I thought maybe a good night's worth of sleep would help me figure this out, but I've gone through all my code and bindings and connections I could recite them in my sleep...

I'm sure the problem I'm making is so small I just keep overlooking it. If someone would be willing to take a quick look at my code, I'd be more than happy to PM a link to the source. Thanks so much, you guys are always saving my arse. :D :rolleyes:
 

jhaspell

macrumors newbie
Feb 17, 2008
7
0
I am also having serious problems with drag and drop of filenames to an NSTableView.

I have been trying the traditional method for several days (as described in Apple's drag and drop tutorial). I was able to read the NSString(s) from the pboard and store them in an NSArray but I could not find any way to link this array to add an entry to my NSTableView.

I then tried using an NSArrayController to make the link between the array and TableView. Despite trying multiple tutorials I couldn't get this working either - still no way to actually update the TableView.

Finally, I found the code posted above by robbieduncan. This works for me and I can now drag and drop to a TableView in my application.

However, I still do not understand how this works. I have been unable to reproduce the bindings in Interface Builder 2.5 - It isn't clear to me which bindings I have to make. The only way I have been able to create another application is to use this project as a base.

Would it be possible for someone to explain the steps required in Interface Builder to make a link between a Controller / NSArrayController / NSMutableArray and NSTableView? Ideally, it would be very helpful if robbieduncan could explain how he produced the Nib behind that example using Interface Builder.

Thanks,

Jon
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I've just redownloaded my code and it all seems pretty standard.

There is a NSMutableArray (not NSArray as you can't add objects to that) to store the filenames (in the Controller class). There is an instance of this class in the main nib.

There is a NSArrayController (in the nib) to manage this array which is bound to the NSMutableArray in the Controller controller class. So this binding links the array controller to the actual data storage.

There is a NSTableView with the Value of the NSTableViewColumn bound to the NSArrayController. So this binding links the content of that column (note we bind the contents of each column separately: you do not bind the values for the whole table) to the content of the NSArrayController, which is the NSMutableArray in our Controller class.

This is more-or-less the layout of the most basic table binding example you can get. I created the example in OSX 10.4, so using IB 2.5...

If you are struggling with NSTableView bindings you could read this tutorial
 

jhaspell

macrumors newbie
Feb 17, 2008
7
0
Thank you for you help robbieduncan. I had already tried similar steps in IB with no success. I've just had another few attempts at following your instructions line by line and am still getting nowhere. I am pretty sure that it is the NSTableView <> NSArrayController binding that is causing my problems.

I spent the afternoon trying to follow that cocoadevcentral tutorial - again being unable to make this work.

I guess I am just missing something fundamental. I will buy the book that Eraserhead mentioned - hopfully that will indicate exactly what to bind: delagate / dataSource/ content / etc.

Thank you again for your help!

Jon
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.