Re: Newbie Question about Document/Window/Text Field
Re: Newbie Question about Document/Window/Text Field
- Subject: Re: Newbie Question about Document/Window/Text Field
- From: mmalcolm crawford <email@hidden>
- Date: Wed, 16 May 2001 00:29:36 +0100
On Tuesday, May 15, 2001, at 09:13 pm, Deirdre Saoirse Moen wrote:
I am trying to create a simple text-editor in my quest to learn Cocoa,
so I've created a project with the document-based app template and
added a big scrolling text field to the window. Bingo! I have a
working text editor. Gotta love that ;)
Yeah, I know -- isn't that part great?
Unfortunately, none of the doc with read/write samples are simple
enough to really show off the structure, which is why I was working on
my complaint example -- a small NSTextField to enter one's complaint
into, etc.
Aw, why not PraiseThemToTheHeavens.app :-)
Where in the documentation can I find answers to such basic questions?
If you're using an NSTextView object for the text field you should
probably:
(a) Keep the actual text as a separate instance variable (NSData
*model), doing proper memory management stuff with it (see later!).
(b) Ensure you have an outlet in your NSDocument subclass connected to it
IBOutlet NSTextView *textView;
(b) Implement dataRepresentationOfType: and
loadDataRepresentation:ofType:
- (NSData *)dataRepresentationOfType:(NSString *)aType {
NSRange range = NSMakeRange(0, [[textView string] length]);
[self setModel: [textView RTFFromRange: range]];
return [self model];
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
NSRange range = NSMakeRange(0, [[textView string] length]);
[self setModel: data];
[textView replaceCharactersInRange: range withRTF: [self model]];
return YES;
}
Clearly you can refactor some of this...
What I've got for reading in:
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)docType
Any particular reason for using this (and using writeToFile: ...) rather
than:
- (BOOL)loadDataRepresentation:(NSData *)docData
ofType:(NSString *)docType
?
Have you provided an implementation of lDR?
Again, right now, I'm not worrying too much (given that I'm writing
teensy files) about the finer points of memory management.
Please, don't ever not worry about the finer points of memory
management. It *will* bite you, honestly.
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
mmalc.