Re: Minimal document-based app
Re: Minimal document-based app
- Subject: Re: Minimal document-based app
- From: ecir hana <email@hidden>
- Date: Wed, 02 May 2012 15:19:29 +0200
Thank you all for the replies!
As I said above, I don't want the question to be whether I should better
use NIBs, or whether Cocoa is more suitable than assembler. I somewhat know
already what the role of NIB is, that it can save time and so on. What I
would like to know is what to do if I was not using NIBs, i.e. if I wanted
to build as much as possible by hand.
To put it in other words, imagine I wanted to go to some city and was
asking about the best spots for hitchhiking. I know it's faster to get
there by a car and that I can use a train. And please, let's not argue
whether I should be hitchhiking in the first place.
Now, I have some code to share!
Please, I think I managed to write an app which seems to be working but I
would like to check whether I missed something. Here what I did:
- I have to use a bundle. That is, a folder with the following structure:
Contents
Contents/Info.plist
Contents/MasOS/<executable>
- Info.plist has to have:
CFBundleExecutable for <executable>
CFBundleName for the name of top-most folder (i.e. the bundle filename)
CFBundleDocumentTypes - which I don't yet understand and will get do it
bellow
- then, the other interesting thing I found out is that I don't need to
write AppDelegate (!). So, my main.m looks like:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSApplication * application = [NSApplication sharedApplication];
<a menu item which fires newDocument:>
[application run];
- then, and this is the interesting bit, newDocument: gets automatically
routed to my NSDocumentClass from Info.plist and there I create the window,
the model, etc.:
- (id)init
{
self = [super init];
if (self) {
NSRect contentSize = NSMakeRect(0.0f, 0.0f, 480.0f, 320.0f);
window = [[NSWindow alloc] initWithContentRect:contentSize
styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:YES];
<...>
[window makeKeyAndOrderFront:self];
}
return self;
}
And when I hit apple-n, the new window pops-up! I also have to implement
dataOfType: and readFromData: - that comes next.
But I have lots of questions:
- I saw that Xcode named the Info.plist differently (it prepends my project
name to it) - is it ok just to call it Info.plist? Is there any convention?
- this CFBundleDocumentTypes - what if I wanted to have only one kind of
documents? Does it have to be an array as well? What is the absolute
minimum every CFBundleDocumentTypes must contain?
- is it really the case that I don't need the AppDelegate? It currently
makes sense to me but maybe I'm missing something?
- I don't want to put the window in init, where else should I put it? Does
NSDocument have an equivalent of applicationDidFinishLaunching, i.e. when
the document is created is there any callback fired?
- new windows are positioned over the previous ones - I know this is
because of that NSMakeRect() - the application created using Xcode (with
NIBs) put every new window slightly to the right, slightly below the
previous window - what is responsible for it? The NIB?
Lots of questions, I know. Thank you in advance for any help with answering
these!
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden