如何显示未归档的自定义NSView对象

时间:2009-05-27 02:09:17

标签: cocoa nsview

我正在学习Cocoa编程,我无法弄清楚如何 归档和取消归档自定义NSView子类

我制作了一个提供窗口的玩具应用程序。这个窗口 包含我的自定义BackgroundView类的实例(存档于 xib文件)。单击此BackgroundView中的任意位置创建和 显示一个蓝色方块,其原点是点击点。这个广场是 我的Square类的一个实例。所有这些都符合我的预期。

Square类实现NSCoding协议。我已经添加 dataOfType:typeName:error:和readfromData:ofType:error:methods 到MyDocument类。据我所知,从日志语句中可以看出 正方形正在归档到文件中,并在文件时取消归档 已加载。但是我无法让方块显示出来 窗户。从未调用Square类的drawWithRect:方法, 即使我在每个广场都调用了setNeedsDisplay:YES。

代码如下:

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

2 个答案:

答案 0 :(得分:4)

由于Square是NSView的子类,而NSView也实现了NSCoding,因此需要调用super的encodeWithCoder:和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

答案 1 :(得分:0)

我可以看到你没有添加任何initWithXXX和encodeWithXXX方法,因为你没有自定义成员变量。

我建议查看Apple的Sketch Example