8

I'm trying to understand how the things in Cocoa works but I'm struggling with one thing. I saw http://cocoawithlove.com/2010/09/minimalist-cocoa-programming.html and http://casperbhansen.wordpress.com/2010/08/15/dev-tip-nibless-development/ and I think I somewhat understood.

Now, I would like to do the same but for a "Document-based application". Please, does anyone know of a tutorial or example akin to the ones above? Alternatively, what do I need to do to make it work? I think I need to create NSDocumentController - but how does it relate to NSApplication? And I create NSDocument from there? Frankly, I'm bit lost...

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117

1 Answers1

10

That was me six months ago! I did not find a decent tutorial either but started with a new project using the default Xcode project template:

enter image description here

I started out with the setup Xcode generates for you when you start a new project and implemented piece by piece as I went along. There's some good reading here on Stackoverflow about the use of the various controller classes but here's what I did:

  • The document class, generated by Xcode, is my top level controller. I do not use NSDocumentController.
  • Each use case of my app has a number of NSViewControllers which manage the various views of the use case. These controllers are dynamically swapped in and out. The top level controllers are managed by the NSDocument class (NSPersistentDocument in my case as I use Core Data).

I am by no means an expert, so I stand corrected for better approaches but so far this setup has been easy to work with, easy to maintain and easy to extend.

Note: Using Core Data is optional but over time I've come to love it and think it's very powerful and a huge time saver. When you decide not to use Core Data, the above setup will still work but you will have to manage your own data.

EDIT: This post explains the relevance of NSDocumentController.

EDIT2: This one from Apple is an interesting read as well.

EDIT3: You do need NIBs (or XIBs as they're now called) as they contain the UI of your app. You pull them in via a view controller (subclass NSViewController):

NSString *aControllerName = [anIdentifier stringByAppendingString: @"ViewController"];
NSString *aNibName = [anIdentifier stringByAppendingString: @"View"];
Class aControllerClass = NSClassFromString(aControllerName);
[self setCurrentController: [[aControllerClass alloc] initWithNibName: aNibName bundle: [NSBundle mainBundle]]];

In the above anIdentifier could be Department, which would instantiate the DepartmentViewController and load the XIB name DeparmentView.

You can use plists to store your data but that's not a requirement. There are many ways to store your apps data. You'll have to read about the various architectures Apple has in place and make your own choices.

Community
  • 1
  • 1
Roger
  • 4,737
  • 4
  • 43
  • 68
  • I try to find the most simple, canonical way, I really liked the tutorials above as they explain the inner working very well. Please, would you mind to post some code? Why didn't you use `NSDocumentController`? And if I wanted it to use, where should I put it? I think I'll have just one view - one window for each document. – Ecir Hana Apr 29 '12 at 22:23
  • I added an explanation about the use of NSDocumentController on a previous question of myself. – Roger Apr 29 '12 at 22:40
  • @EcirHana: See also my comment on the accepted answer on Roger's question. – Peter Hosey Apr 29 '12 at 23:22
  • @Peter Hosey: That is actually a very useful comment. I would be tempted to classify it as answer :-) – Roger Apr 29 '12 at 23:33
  • Guys, I apologize but I don't follow. Would you be please so kind and post some code? It would be great to have a reference similar to the tutorials above. – Ecir Hana Apr 30 '12 at 05:57
  • I added another link to a piece by Apple about their document architecture. I cannot post code unfortunately. Have you tried to create a new project from scratch and investigate the code Xcode generates for you? – Roger Apr 30 '12 at 08:21
  • 1
    @Roger: yes, this is exactly how I began. But then I got lost, because it uses NIBs and plists and there is not NSDocumentController even that there should be, ... Do I need any bundles to make it work? – Ecir Hana Apr 30 '12 at 08:27