Swift 1.2 drag and drop controls and views in Cocoa - swift

I'm building one application where I need to make a simple window and be able to drag&drop files onto this window. In this app I have to then parse file path and write out all paths of dropped files in table or nice order (one under another). App doesn't need to take more than one file at once, but this might be a feature for future.
Up to now I have managed to make simple drag&drop in NSView (which I extended with custom class). App is correctly registering drag&drop events and I get all file path and I have also figured how to get file names out of those strings.
My question is: How can I move this "drag&drop" to NSTableView, as I wish to have file names in nicely in table?
I have tried to put tableview under NSView but it doesn't work and if I extend NSTableView class it just registers that there was drag&drop event but nothing else happens.
Do I have to extend other class or how to make this work? I'm implementing NSDraggingDestination protocol in custom class and using performDragOperation function.
I would like to have:
- whole app window can take drag&drop-ed files, not matter where I drop it it has to accept file and put file name into table
I'm sorry if this might sound trivial but I have just started learning swift and developing OS X apps and I'm not used to this new API and don't really know how all things works here.

Related

Drag & drop in custom tree view [duplicate]

I have successfully populated a TreeView via creating a TreeDataProvider and customized the icons and its collapse-able property. Now I want to be able to drag and drop its ViewItem(s) onto a WebView. There are unfortunately no samples for drag and drop. Reading through the TreeView source, ViewItem(s) are created in HeightMap.OnInsertItems() via the TreeView.createViewItem(), from the derived class.
The public TreeView.onInsertItem() would appear to be a great extension point, extending TreeView and onInsertItem() and capturing its ViewItem parameter. This Unfortunately, the creation of the concrete class, ExtHostTreeView, that is ultimately created, is buried in layers of private methods (createExtHostTreeViewer()).
Strangely, the ViewItem has a setter for draggable. However, how would you ever get a reference to the ViewItem to set it? If there is a way to do this please point me at an example. If there is not, I would be glad to fork and implement it. Leaving the existing onInsertItem and perhaps add an array of handlers to be added to with an addOnInsertItemListener() method? Or perhaps use RxJS?
There are so many places this could be used. You could drag template fragments into documents or code, DSL rules into a expert system, configuration file values into parameters or right hand values (generating the code to retrieve the value). Thank you for any assistance.
It seems that drag and drop in custom views is currently not supported. There's a (fairly popular) feature request for it here:
Add drag and drop for contributed tree views (#32592)
As of version 1.66 (March 2022) there is a TreeDragAndDropController which can be used for some drag & drop actions.
In April 2022, their sample focuses on drag & drop within a tree view: https://github.com/microsoft/vscode-extension-samples/blob/main/tree-view-sample/src/testViewDragAndDrop.ts
It is also possible to use the text/url-list type as described in the documentation to allow for dragging files to the editor panel: https://github.com/microsoft/vscode/blob/dc2f5d8dd1790ac4fc6054e11b44e36884caa4be/src/vscode-dts/vscode.d.ts#L9843-L9859
It is not clear to me yet whether it is possible to drag arbitrary data to webviews or to use standard HTML5 drag & drop API in a webview to allow dragging data to a custom tree view.

Creating a List of Custom Views in Xcode

I'm currently working on developing a desktop application for MacOS for downloading batches of audio files from URLs at one time I've run into a question about UI design that I can't figure out.
I have a class called SongEntry.swift that holds information regarding the URL that was entered (e.g. url, title, length, author, etc.) and I want to create a vertically growing list of custom views that updates when a new one is added.
Where is what the base view looks like:
Inside of the big white area is where I want the list to be held.
I've tried to create a separate view controller that handles each entry but that didn't work. I know UI design for MacOS is much different from iOS, however I think what I'm looking for is a way to simulate the table views and cell prototypes from iOS but can't find a suitable option.
If anyone knows of a possible solution or can point me in the right direction, I'd greatly appreciate it!
What you want is one of the collection views. For vertical list, you'll probably use NSTableView with only one column and hide everything else like headers.
Here are roughly the steps you need:
You can use your existing view controller or create a dedicate view controller for just the table (and use the 'embed' option in Interface Builder)
This view controller will adopt the NSTableViewDataSource and NSTableViewDelegate protocols to provide the data (your SongEntry objects) and the view for each row.
You set your NSTableView's source and delegate to your view controller.
You create a view which will serve as your "cell", it will be used by each row to display the data. You can design it either in IB or in code.
The entire process is described in detail in the Table View Programming Guide for Mac.
This topic can be a bit confusing. Note that there are two main approaches: the view-based and NSCell-based tables. Don't bother with the NSCell way, it's more of a legacy leftovers.
Also note that there are some overlap of methods in both NSTableViewDataSource and NSTableViewDelegate to provide data and views that can be a bit confusing at first. Just play around with the code and samples and it will be clearer which delegate method to use when.
There are many examples both on Apple's developer site and github. Also a good tutorial on raywenderlich.com.

XCode wont let me use a Static Library View in my xib files

I created a new project of type Static Linked Library.
I have moved the code (previously working in the app) to the library leaving all the xib files in the app.
I have changed the export directory and added the library file from the derived data folder so it gets the compiled version and links to it.
I have added the library to the bundle and have verified it is in the app using iPhone Explorer
I have verified that the xib file recognized the new view type by removing and selecting the custom view type in the custom class field of the identity inspector.
I have changed all of the #import commands to reflect the <> VS "" change and have gotten the app to compile.
however none of the methods of my custom class can be executed. and an NSLog reports that the class is its base counterpart as opposed to the custom derived class.
I am at a loss and have spend a long time converting all the code I have to work in a library. I am hoping someone can help with this.
On this issue, it turns out that Objective C's compiler does not load the categories and custom classes in the libraries by default. There are 2 ways to overcome this.
And here are your options.
Create an Activation type method on the category that will be called to "Activate" that Class or Category. This will need to be done before the application loads any nib files. And seems like a bit of a pain in the butt. This was however how I originally overcame this issue.
Add linker flags that will tell objective C to load all libraries and attach any and all Categories and Classes within the libraries before executing the application. (this will need to be done, on top of #import(ing) the correct files from their correct locations. I will provide a graphic below. and provide the flags for search purposes
-ObjC is the first one I added, but -all_load really did the trick.
I hope that others can benefit from this experience.

iOS - QuickLook - How to open an object in QuickLook without a UIScrollView

Could anyone point me towards a resource which uses QuickLook to open a (preferably but not necessarily a pdf) file without using a UITableView?
I do have this example of using QuickLook but it uses a listview which I need to get away from.
http://robsprogramknowledge.blogspot.com/2011/02/quick-look-for-ios_21.html
I'm not sure how you plan to design your UI to open a file. I've used a few different ways, so I'll toss out some ideas. A UITableView is ideal for large amounts of files. A generic scroll view can also be used for a large number of files. I've used an alert view for an app that only generates one or two files. You could also use a view with document icons like the iPad Mail app. To get the document icons, use UIDocumentInteractionController. The WWDC 2010 DocInteraction sample code goes in great depth with how to use UIDocumentInteractionController.
As for opening the file, the Quick Look framework makes that easy. A simple, self-contained solution is to subclass QLPreviewController. Then, your subclass needs to conform to the QLPreviewControllerDataSource protocol and optionally the QLPreviewControllerDelegate protocol. Next, pass it an array of NSURLs pointing to your files. You can do this either through an initializer like -initWithFiles:(NSArray *)files or through a setter. From here, -previewController:previewItemAtIndex: just needs to index into the array to get the appropriate file to show. -numberOfPreviewItemsInPreviewController: just needs to return the size of the array. Once you have this class finished, you can use any UI design you like to push this view or present it modally.
Hopefully this is more clear than my tutorial you've been reading.
EDIT:
I have posted some code to Github that may help you. I have created a file previewer class as described above. I also posted a demo app that directly uses a QLPreviewController.

Iphone sdk - How to setup a 'template'

I've been working on a Cook Book App and I've been making each page individually which takes a really long time to do, I asked a question similar to this and it was brought to my attention that you can setup a way to automate the design process so all you need to do is input your data.
Can someone please explain in as much detail as possible how you setup your xcode files/code to automate such a process
So for example I would just enter the page text and it would automatically put my standard background picture in and add a scroll view and appropriate buttons etc.
Thanks
You could make one master view that contains all the controls that you need: standard background picture, scroll view, appropriate buttons, etc, and make any subsequent views that you create inherit from this view, so that they all contain those controls.
You could also use just one view and work with multiple instances of it, one instance per page. Just make sure to have a Text property on it, or a constructor that takes in your text string, so that you could set it to a different text on each page.
Xcode project templates and file templates are pretty easy to make, with a few caveats.
Check the answers to these questions:
Add new templates in Xcode
Change templates in XCode
Also take a gander at these handy tutorials:
Custom Xcode Templates
Xcode: How to customize the existing project templates
It sounds to me like your putting your data into your views (pages). That's a big design error. You need to employ the Model-View-Controller design pattern and separate your data from your views. That will make it easy to create one view (template) that you can reload with data to display each individual recipe.
The first thing to do is to separate your data from the view. You need to have the recipes stored in an array, dictionary, Core Data etc and then wrap that data in a dedicated object. The second thing to do is to create a dedicated view to display all the recipes. As the user moves from recipe to recipe the app will simply remove and add information to the same view as needed.
I would recommend Cocoa Recipes for Mac OS X: The Vermont Recipes, Second Edition because it addresses these issues and it uses a recipe type app as its example. It's for Cocoa but the basic principles apply to iPhone apps as well.