How can I make a copy of an already created window? - objective-c

So I am making an xcode application for OS X, and so far things have gone well. However, right now I am at a roadblock. My app is mostly one window, and I need that window to be created multiple times, in the way that apps like Safari, TextEdit, and all those ones have the File>New function. I am new at Xcode, and I was wondering if there was an easy way (hopefully 3-4 lines of code) to do this. If someone could explain that to me that would be great.
Thanks!

Start a new OS X project and choose "Create Document Based Application" . This will create a NSDocument subclass.
If you want to use Core Data , choose that also. This will create a NSPersistentDocument subclass.
This will give you the basis of your application with all the functionality you ask for.
Have a look at Document Based App Programming guide and NSPersistentDocument Class Reference
Whatever you do, dont try to bake your own. There lies madness.
The project template sets up the info.plist correctly to make this work.

Whatever I tell you, it won't involve only "3-4 lines of code," unfortunately.
If you're working from a non-document-based application, you'll want to factor out the code that creates your one window. If you're not using a window controller in concert with your window, you should implement one. Then you can implement an action that will create a new instance of your window controller, and so a new window. Of course, you'll need to add more infrastructure if you're interested in saving information associated with a window to a file.
Which leads me to your other option, which is to create a document-based application. The big advantage here is that, if you're interested in saving the window contents to a file, the infrastructure for doing so is provided. This involves starting over to some extent, but since you've already got the guts of your application working, you'd simply transfer those guts to the document subclass that is provided for you. You'd also need to provide to your project meta information describing the kind of document it is, including the file extension describing it.
Best wishes to you in your endeavors.

Related

How to implement the "New Document" window like Pages app does?

As a OS X user, you must be quite familiar with the "New Documents" window when you start some Apple's apps, like Pages.
This window is so common, that I thought it must be some system-provided window, am I right?
I tried to mimic the window, and it turns out to be quite tedious. As a developer, I have to first create a finder-like browser view with list-style selection support. Also, iCloud integration is quite difficult not to mention search support.
So, my question is how can I implement this window, is there any documentation writing about this window? Big thanks!
Look at NSDocument. That is Apple's suggested way of creating document-based apps. You create a subclass of NSDocument to handle tasks specific to your data format, and the superclass handles all the heavy lifting, including iCloud support. I haven't used it myself, but I know that it handles open and save chores, so that's probably where the panel is coming from.
As it turns out, to get this window, all I need to do is to enable iCloud support in Capabilities tab of project properties page.
A little heads up: doesn't work for Swift in Xcode6 Beta. I don't know why to be frankly. And it took me a day to searching around...

Xcode for Mac Programming.. Are there "standard" templates?

I notice that Mail.app, iTunes and even Finder have a similar template, with a various columns, but often, a left Column with "folders" and "files". Is this a sort of standard template, that I can use so as to not reinvent the wheel?
Thanks!!
No, Apple does not provide a control that acts like a finder window, etc., although it is a common request and you might want to file a bug report at http://bugreport.apple.com making this request...it would help push them along to provide such a standard control.
However, there are several controls that implement various aspects that you may want to take a look at like NSTableView, NSOutlineView, NSCollectionView & IKImageBrowser.
Although, to get everything you want, you may have to reinvent the wheel. I have generally found these control hard to customize if they do not provide everything you need out-of-the-box...but they do provide a lot out-of-the-box.
Nopes, but you can create your own templates so you only have to reinvent the wheel once.
By the way, you can decompile nibs from some applications by copying two files from an empty nib into them.
By the way, if you decompile Finder's nibs, you see that they are very messy and the sidebar is in a separate nib. I guess they are combined programmatically.
By the way, iTunes uses Carbon nibs while Finder and Mail use Cocoa nibs.
By the way, Mail is soon to be replaced with a new version that has a totally different lay-out.

Alternative to NSCollectionView in pre-OSX10.5, Cocotron?

NSCollectionView was introduced in OS X 10.5, and is not yet implemented in Cocotron.
I am trying to implement a small app that will allow creating properly packaged data files for an online service, which will then be uploaded by an administrator. More specifically, the user will create a collection of input and output data pairs, by dragging input and output files onto the window.
Currently the idea is that user drags a file, from the filename it's detected if it's the input or output filename (by default, input), and a view with icon and filename for input and output is added to collection view. Then, the second file is dropped on the "other" icon.
However, NSCollectionView does not appear in pre-10.5, and most of my users don't have Macs so I'll have to provide a Cocotron-built application. Not only that; I still don't fully understand KVC/KVO, and I really should understand everything that my code does. Hence, I need an alternative to NSCollectionView.
What alternative do I have to using NSCollectionView? (Any intuitive solution is appreciated, don't feel limited by the above description of my idea.)
To work with NSCollectionView, you need to not only understand KVC and KVO, but also Bindings.
There's code for an NSCollectionView clone that works on Tiger here.
I still don't fully understand KVC/KVO…
That's what the docs are for:
Key-Value Coding Programming Guide
Key-Value Observing Programming Guide
What alternative do I have to using NSCollectionView?
Make your own.

Creating a Multiwindowed Cocoa Program - Launching Procedure Suggestions?

I'm porting an application I developed in Visual Studio 2008 over to Cocoa. I'm currently doing a 'learn-as-you-go' approach to Cocoa, so I can experiment with different ideas and techniques in smaller, simpler projects and eventually combine them into one big application.
My program logic is as follows (in a dumbed-down sense). Items in the list are mandated by my boss.
Application is started
1a. Verify CD program is in drive.
Verify license. If found and is valid, skip to step 7
Display license agreement.
Display serial number prompt.
Verify and save serial number.
Hide all prior windows.
Load main application window
Intercept requests and commands from main application window, including making a duplicate main application window
Exit program when requested by user
What would the best bet be for this type of application? From another question I asked, I found out that I should keep the 'main application' window in a separate XIB file from the rest, because I might need to clone and interact with it.
I know that since Cocoa and Objective-C is based off of C, there is a Main method somewhere. But what would you all suggest as a starting place for an application like this?
So some of this comes down to organization. Like Julien mentioned, you'll want a YouappnameApplicationDelegate class - in fact, Xcode will create and set this up for you when you create a project.
implement the applicationDidFinishLaunching method (which also should be provided by what Xcode gave you), and implement your logic for steps 1 and 2 there (and steps 7, 8, 9).
Steps 3 thru 5 you'll probably want to implement in another class. RegistrationWindowController, or something like that. You may even want to create this window in another nib file (not inside the MainMenu.nib file that Xcode gives you). In the YouappnameApplicationDelegation's applicationDidFinishLaunching method you'd load this nib (see code sample below). That way your code is well organized - which is what Cocoa really guides you to do.
Ok, so now how to load that new nib file:
myInstanceVariable = [[RegistrationWindowController alloc] initWithWindowNibName: #"MyNibName"];
[myInstanceVariable showWindow: self];
RegistrationWindowController should be a subclass of NSWindowController.
That should take you a pretty long way into your research, hope it does help!

Document-based app in one window

I need to develop a simple document-based application but using only one window, the documents should open in views separated by tabs.
How can I achieve it?
The answer is you can achieve what you want with a lot of hackery to shoehorn the Cooca NSDocument architecture into a single-window style. But you really don't want to do that. It's much easier to write your own system for document handling from scratch. Start with the Non-document based application template and go from there. NSDocument expects to have its own window (and associated NSWindowControllers).