I've been learning Cocoa on OS X lately. One of the things I've been experimenting with is document based applications. I've been working on an application that processes Par and Par2 files. So, in the process of developing this application, I needed to write a document based application to view the contents of the files. The problem is, XCode doesn't prompt you for the name of the document class when you tell it to generate a new Document based application. Instead, it generates a document class called 'MyDocument'. This is obviously not a very useful class name. When XCode generates this MyDocument class, it also generates the MyDocument.nib file and connects everything together for you. The problem is, if you try to rename the document, all those connections break and the document and NIB stop working together. Plus, if you want to generate a second document/NIB, you are on your own. I wanted to document how to manually rename a Document class/NIB so that the connections between them continue to work. For this example, I'll use the example of renaming 'MyDocument' to 'ParDocument' (which is what I did in my application). If you want to use a different class name, just substitute your class name for everywhere that I mention 'ParDocument'.
Generate the new Document class
- In the File menu, select 'New File'.
- In the Assistant dialog, choose the 'Objective-C NSDocument subclass' item under 'Cocoa'.
- Give the new class the filename 'ParDocument.m'. Make sure that the 'Also create "ParDocument.h" is checked.
- If you view the contents of the ParDocument.m file and look at the 'windowNibName' function, you will see that it returns @"ParDocument". This string returned MUST match the filename of the NIB file that goes with this document. In other words, it will try to load 'ParDocument.nib', since you listed 'ParDocument' as the Nib file name. If you rename the class in the future, you also need to change this string to point to the new Nib file. Since we generated the class, this string is already set correctly.
Generate the new NIB file
- Open Interface Builder by double clicking on the MyDocument.nib file.
- In Interface Builder's File menu, choose 'New'.
- In the 'Starting Point' Dialog, choose 'Window' under the 'Cocoa' item.
- In Interface Builder's File menu, choose Save As.
- Make sure that you choose the right directory here! Save it to the 'English.lproj' directory underneath your main application directory.
- If you save it to this directory, it will then prompt you to add the new NIB file to your project. Tell it to add it to your project.
- From Interface Builder's Classes menu, choose 'Read Files'
- Select the 'ParDocument.h' file. This is necessary to tell IB about your Document class.
- In IB, click on the 'File Owner' icon.
- In the Tools menu, pick Show Inspector.
- Choose 'Custom Class' from the Inspector's top drop down list. Select ParDocument as the class.
- In IB, Control-Drag from the 'Files Owner' icon to the 'Window' icon. Set the 'window' connection to this Window.
- In IB, Control-Drag from the 'Window' icon to the 'File's Owner' icon. Set the 'delegate' connection to ParDocument.
- Save and exit IB
Set the Document Class for the Document
- In Xcode, right clock on the Target and select GetInfo
- Set the 'Class' column of the list on the bottom to ParDocument
That's it! Now that everything is connected for your new ParDocument class, you can go ahead and delete MyDocument.h, MyDocument.m and MyDocument.nib from the project.
It's a long, involved, and very confusing process to do this. Hopefully this has helped you.