Tutorial/example of a minimal document-based app

瘦欲@ 提交于 2019-12-04 00:57:57

问题


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...


回答1:


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:

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.



来源:https://stackoverflow.com/questions/10373304/tutorial-example-of-a-minimal-document-based-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!