Hello,About encoding and decoding custom View question.

Discussion in 'Mac Programming' started by phienex, Apr 9, 2012.

  1. phienex, Apr 9, 2012
    Last edited by a moderator: Apr 10, 2012

    phienex macrumors newbie

    Apr 9, 2012
    Hello,I am freshman in here..and my english is not good, sorry . I want to encoding and decoding my custom view..But, I can't ,I had waste several month about it...Anybody can help me?thank you very much..Below is my code..

    In my custom view: I define a custom view such as:
    @class MyDocument;
    @interface StretchView : NSView
    	NSPoint points[NUM];
    	NSPoint q[NUM];
    	NSMutableArray *rectArray;
    	NSPoint p1,p2,p3,p4;
    	NSInteger selectedSegment;
    	NSPoint location;
    	CGPoint _point1;
    	CGPoint _controlPoint1;
    	CGPoint _point2;
    	CGPoint _controlPoint2;
        NSAffineTransform *transform;
        float control;
    	float step;
        float control2;
        float step2;
        IBOutlet MyDocument *document;
        //draw Text:
        NSTextStorage *textStorage;
    	NSLayoutManager *layoutManager;
    	NSTextContainer *textContainer;
        IBOutlet NSTextField *myTextField;
        NSBezierPath *newPath;
        NSImage *image;
    And I want to save the custom view in MyDocument class, and I design the UI include button ,when I press button ,it can add custom view to display in window. So, In my mydocument class code:


    In MyDocument class:
    - (id)init
        self = [super init];
        if (self) {
            squiggleArray=[[NSMutableArray alloc] init];
        return self;
    - (NSString *)windowNibName
        return @"MyDocument";
    - (void)windowControllerDidLoadNib:(NSWindowController *)aController
        [super windowControllerDidLoadNib:aController];
        [stretchView setNsImageObj:image];
    	MyWindowController *windowController = [[MyWindowController alloc] init];
    	[self addWindowController:windowController];
    	[windowController release];
    -(NSArray *)squiggles
    	return [NSArray arrayWithArray:squiggleArray];
    - (void)addSquiggle:(Squiggle *)squiggle 
        [[[self undoManager] prepareWithInvocationTarget:self] removeSquiggle:squiggle];
        [squiggleArray addObject:squiggle];
        [stretchView setNeedsDisplay:YES];
    - (void)removeSquiggle:(Squiggle *)squiggle 
        [[[self undoManager] prepareWithInvocationTarget:self] addSquiggle:squiggle];
        [squiggleArray removeObject:squiggle];
        [stretchView setNeedsDisplay:YES];
    - (void)startNewSquiggleWithPoint:(NSPoint)point 
        Squiggle *squiggle = [[Squiggle alloc] init];
        [squiggle addPoint:point];
        [self addSquiggle:squiggle];
    - (Squiggle *)currentSquiggle 
        return [squiggleArray lastObject];
    	myViewController = [[MyViewController alloc] initWithNibName:@"MyDocument" bundle:nil];
    	zFrame1 = [myViewController.view frame];
    	[myViewController.view setFrame:zFrame1];
    	[myViewController.view setFrameOrigin:nsPointViewOrigin];
    	press = [nsMutableView count];
    	[nsWindowController.window.contentView addSubview:myViewController.view];
    	[nsMutableView addObject:myViewController];	
    - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
    	NSMutableDictionary *documentDictionary = [NSMutableDictionary dictionary];
        [documentDictionary setObject:[NSKeyedArchiver archivedDataWithRootObject:squiggleArray] forKey:@"squiggles"];    
        [documentDictionary setObject:[NSKeyedArchiver archivedDataWithRootObject:stretchView.nsImageObj] forKey:@"nsImageObj"];
    	StretchView *newView = [[StretchView alloc] init];
    	[documentDictionary setObject:[NSKeyedArchiver archivedDataWithRootObject:newView] forKey:@"newView"];
    	return [NSPropertyListSerialization dataFromPropertyList:documentDictionary format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
    - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
    	NSDictionary *properties = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL];
        [squiggleArray setArray:[NSKeyedUnarchiver unarchiveObjectWithData:[properties objectForKey:@"squiggles"]]];
    	image = (NSImage *)[NSKeyedUnarchiver unarchiveObjectWithData:[properties objectForKey:@"nsImageObj"]];
        return YES;
  2. IDMah macrumors 6502


    May 13, 2011
    you don't

    You don't save your view you save the data that creates your view.
    and then rebuild your view with the loaded data..

    I know this can be confusing.. but encode has a very limited types of things it can save.


    I basically used the Hillegass book example and adapted it..
    all the objects have to know how to encode themselves to be successful.

    Hope this helps but more than likely you have solved this already.

Share This Page