Re: readFromURL:url options: not working for me
Re: readFromURL:url options: not working for me
- Subject: Re: readFromURL:url options: not working for me
- From: "Louis C. Sacha" <email@hidden>
- Date: Sat, 17 Jul 2004 22:52:42 -0700
Hello...
Hopefully someone else has replied to this offlist or you have
figured it out, but just in case...
The problem you are having is probably related to the fact that the
file is read in by your NSDocument subclass before the nib for the
document has been loaded, so the outlets to objects in the nib have
not yet been set.
Since your textView outlet would be nil if the nib has not yet
loaded, when you request its text storage and layout manager both
end up being nil as well, so you are sending lots of messages nowhere
:)
When you revert in a document, the nib has previously been loaded, so
the outlets from the nib are already set when the previously saved
version of the file is read in.
There are two general ways of handling this. One is by having a
model within your document class that is independent of the
view/interface loaded from the nib, and then refreshing the interface
when the nib is loaded and anytime the data is read in. The second is
to cache the information somewhere when the outlets from the nib
aren't available, and then load it when appropriate.
For example, if you decided to take the second approach (which is
probably easier in your case since the text view has its own model),
you could add an instance variable(s) to cache the attributed string
and document attributes if you don't already have an appropriate ivar
to cache them in, and separate out the code that updates the
interface so it can more easily be called from several locations.
/* untested, typed in mail, and so forth... */
The following code assumes that your document class has an added
instance variable called documentCache that is an NSMutableDictionary
created in your document's init method and released in dealloc. You
probably want to keep the document attributes dictionary around (so
that you can use them when saving your document, and avoid losing any
other existing attributes when your app edits and saves the document)
so they are left in the documentCache dictionary even after the
interface is updated.
So, based on the existing code that you posted, it would work
something like this...
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)docType
{
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:NSRTFDTextDocumentType forKey:@"DocumentType"];
NSURL *url = [NSURL fileURLWithPath:fileName];
NSMutableAttributedString *content =
[[[NSMutableAttributedString alloc] init] autorelease];
NSDictionary *docAttrs;
BOOL success = [content readFromURL:url options:options
documentAttributes:&docAttrs];
if (!success) return NO;
/* save the document attributes to documentCache */
[documentCache setObject:docAttrs forKey:@"RTFDDocumentAttributes"];
/* if the interface has already loaded, go ahead and load
content into the text view, otherwise cache for later */
if (textView) {[self
updateInterfaceForDocumentContent:content attributes:docAttrs];}
else {[documentCache setObject:content
forKey:@"RTFDDocumentTempContentCache"];}
return YES;
}
- (void)updateInterfaceForDocumentContent:(NSAttributedString
*)content attributes:(NSDictionary *)attributes
{
NSTextStorage *storage = [textView textStorage];
NSLayoutManager *layoutManager = [textView layoutManager];
/* does removing the layout manager really help performance
that much if the whole contents are being replaced? */
[layoutManager retain];
[storage removeLayoutManager:layoutManager];
[storage beginEditing];
[storage setAttributedString:content];
[storage endEditing];
[storage addLayoutManager:layoutManager];
[layoutManager release];
NSColor *background = [attributes objectForKey:@"BackgroundColor"];
if (background) {[textView setBackgroundColor:background];}
}
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController
/* if you have multiple window controllers associated with each
document instance, you may need to conditionalize this to be
triggered by the correct window controller... */
{
/* if the document content has been temporarily cached, load
it into the textview, and then remove it from the cache */
NSAttributedString *content = [documentCache
objectForKey:@"RTFDDocumentTempContentCache"];
if (content)
{
NSDictionary *attributes = [documentCache
objectForKey:@"RTFDDocumentAttributes"];
[self updateInterfaceForDocumentContent:content
attributes:attributes];
[documentCache
removeObjectForKey:@"RTFDDocumentTempContentCache"];
}
}
Hope that helps,
Louis
I am trying to open an RTFD file in such a way as to display the
document's bg color.
Files that I save cannot be opened but the code works with Revert.
i.e. When I create a file, save it , change the open file and choose
Revert, the following code executes and the document reverts to the
saved version.
However, when this code is run upon Opening a file, the
"readFromURL:url" does not work.
Can someone shed some light on this. I just don't know what I'm doing wrong.
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)docType
{
...
}
Thanks,
David
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.