Tutorial/example of a minimal document-based app

后端 未结 1 741
无人共我
无人共我 2021-01-12 07:44

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

相关标签:
1条回答
  • 2021-01-12 08:09

    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.

    0 讨论(0)
提交回复
热议问题