On Dec 18, 2010, at 10:38 PM, Andy Lee wrote:
> As a learning experience, I'm trying to make a trivial document-based app: a 
> window with a text view that can edit and save RTF files.
> 
> What I have now almost works, except that whenever I save changes, the text 
> view scrolls to the top.  Needless to say, this would be very annoying in a 
> real text-editing app.  I suspect it's something trivial about document-based 
> apps and/or bindings that I just don't understand.  Can someone explain 
> what's going on?

Following up... I found Apple's equivalent of what I was doing: 
<http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextArchitecture/Tasks/TextEditor.html>.
  It doesn't use bindings.  When I followed the instructions the resulting app 
worked fine.

However, it bugged me that it has an ivar that sometimes is a copy, possibly 
out of date, of the text view's contents.  It seemed like unnecessary 
duplication of data to me, so I tweaked the example to use an NSTextStorage for 
the ivar, which it then plugs into the text view.  The resulting document class 
has fewer lines of code, even with the missing dealloc that I added.  And it 
doesn't need to be the delegate of the text view, so even the nib is a teeny 
bit simpler.

I can understand why Apple wouldn't use a bindings-based solution for this 
example, since bindings is an advanced topic unto itself, but I think using an 
NSTextStorage would be appropriate given the context.  Unless someone spots a 
fatal flaw, I'll submit it as a doc suggestion.

Here's the code I ended up with.  I never did figure out why my bindings-based 
app scrolls to the top.

--Andy


//  MyDocument.h

#import <Cocoa/Cocoa.h>

@interface MyDocument : NSDocument
{
    NSTextStorage *mString;
    
    IBOutlet NSTextView *textView;
}

@end



//  MyDocument.m

#import "MyDocument.h"

@implementation MyDocument

- (id)init
{
    self = [super init];
    if (self)
    {
        mString = [[NSTextStorage alloc] init];
    }
    
    return self;
}

- (void)dealloc
{
    [mString release];

    [super dealloc];
}

- (NSString *)windowNibName
{
    return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    
    if (mString)
    {
        [[textView layoutManager] replaceTextStorage:mString];
    }
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    [textView breakUndoCoalescing];
    
    return [mString dataFromRange:NSMakeRange(0, [mString length])    
               documentAttributes:[NSDictionary 
dictionaryWithObject:NSRTFTextDocumentType                                      
     
                                                              
forKey:NSDocumentTypeDocumentAttribute]
                            error:outError];
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError 
**)outError
{
    [mString release];
    mString = [[NSTextStorage alloc] initWithData:data
                                          options:nil                    
                               documentAttributes:NULL
                                            error:outError];
    
    return (mString != nil);
}

@end

_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to