教程/基于文档的最小应用程序的示例 [英] Tutorial/example of a minimal document-based app

查看:137
本文介绍了教程/基于文档的最小应用程序的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解Cocoa中的东西是如何工作的,但我一直在努力。我看到了说明了NSDocumentController的相关性。



EDIT2:这一个



EDIT3:你需要NIB(或XIB,因为它们现在被称为),因为它们包含UI您的应用程式。您通过视图控制器(子类 NSViewController )拉入它们:

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

在上述 anIdentifier 中可以是 Department ,它将实例化 DepartmentViewController 并加载XIB名称 DeparmentView



您可以使用plists存储您的数据,但这不是必需的。有很多方法可以存储您的应用数据。你必须阅读苹果已经建立的各种架构,并做出自己的选择。


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

解决方案

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.

这篇关于教程/基于文档的最小应用程序的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆