How to display unarchived custom NSView objects
I am involved with Cocoa programming and Im not able to figure out how to archive and unlock a custom NSView subclass
I've created a toy app that presents a window. This window contains an instance of my custom BackgroundView class (archived xib file). Clicking anywhere on this BackgroundView creates and displays a blue square that originates from the clicked point. This square is an instance of my square class. This all works as I expect.
The Square class implements the NSCoding protocol. I added dataOfType: typeName: error: and readfromData: ofType: error: methods to the MyDocument class. As far as I can tell from the log statements, Squares are zipped to a file and unzipped when the file is downloaded. But I was unable to get the squares to display themselves on the window. Square's drawWithRect: method never gets called, although I've called setNeedsDisplay: YES on every square.
The code looks like this:
Square.h
#import <Cocoa/Cocoa.h>
@interface Square : NSView <NSCoding> {
}
@end
Square.m
#import "Square.h"
@implementation Square
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
return self;
}
- (void)drawRect:(NSRect)rect {
[[NSColor blueColor] set];
NSBezierPath *newPath = [NSBezierPath bezierPathWithRect:[self bounds]];
[newPath fill];
}
-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeRect:[self frame] forKey:@"frame"];
}
-(id)initWithCoder:(NSCoder *)coder
{
NSRect theRect = [coder decodeRectForKey:@"frame"];
self = [super initWithFrame:theRect];
return self;
}
@end
-----
BackgroundView.h
#import <Cocoa/Cocoa.h>
@interface BackgroundView : NSView {
}
@end
BackgroundView.m
#import "BackgroundView.h"
#import "Square.h"
@implementation BackgroundView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)rect {
for (Square *subview in self.subviews)
[subview setNeedsDisplay:YES];
}
-(void)mouseDown:(NSEvent *)theEvent {
NSPoint unsetClick = [theEvent locationInWindow];
NSPoint theClick = [self convertPoint:unsetClick fromView:nil];
NSRect theRect;
theRect.origin = theClick;
theRect.size.width = 100.00;
theRect.size.height = 100.00;
Square *newSquare = [[Square alloc] initWithFrame:theRect];
[self addSubview:newSquare];
[newSquare setNeedsDisplay:YES];
}
@end
------
MyDocument.h
#import <Cocoa/Cocoa.h>
@class BackgroundView;
@interface MyDocument : NSDocument
{
IBOutlet BackgroundView *theBackgroundView;
}
@end
MyDocument.m
#import "MyDocument.h"
@implementation MyDocument
- (id)init
{
self = [super init];
return self;
}
- (NSString *)windowNibName
{
return @"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
return [NSKeyedArchiver
archivedDataWithRootObject:[theBackgroundView subviews]];
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain
code:unimpErr userInfo:NULL];
}
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName
error:(NSError **)outError
{
[theBackgroundView setSubviews:[NSKeyedUnarchiver
unarchiveObjectWithData:data]];
[theBackgroundView setNeedsDisplay:YES];
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain
code:unimpErr userInfo:NULL];
}
return YES;
}
@end
a source to share
Since Square is a subclass of NSView, and NSView also implements NSCoding, you need to call the super implementation of both encodeWithCoder: and initWithCoder :.
@implementation Square
- (id)initWithFrame:(NSRect)frame
{
return [super initWithFrame:frame];
}
-(id)initWithCoder:(NSCoder *)coder
{
if ((self = [super initWithCoder:coder]))
{
NSRect theRect = [coder decodeRectForKey:@"frame"];
self = [super initWithFrame:theRect];
return self;
}
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeRect:[self frame] forKey:@"frame"];
}
- (void)drawRect:(NSRect)rect
{
[[NSColor blueColor] set];
[[NSBezierPath bezierPathWithRect:[self bounds]] fill];
}
@end
a source to share
As far as I can see you are not adding any initWiThanksXX and encodeWiThanksXX methods as you have no custom member variables.
I suggest checking out Apple's Sample Sketch .
a source to share