编辑:问题已经解决,我将变量的分配和启动移到了另一种方法中。SubOtherClass
永远不会被启动(alloc
并且init
永远不会被调用)。
类已被重命名以使这个问题更普遍。
假设类OtherClass
扩展NSView
假设类SubOtherClass
扩展了假设类OtherClass
并在本地实例中调用更新方法ClassToUpdate
我知道在释放键时更新视图不是最好的想法,但这只是暂时的。我不是 Obj-C 方面的专家。重复这个问题,更新方法 inSubOtherClass
被执行但不是 in ClassToUpdate
,并且该方法的内容(此处未显示)不运行。我怎样才能解决这个问题?如果需要更多信息,请询问。
谢谢。
编辑:完整代码(重命名的类)
标题:
#import "OtherClass.h"
#import "ThingToRender.h"
#import "ClassToUpdate.h"
@interface SubOtherClass : OtherClass
@property (assign) ThingToRender *thingToRender1, *thingToRender2;
@property (retain) ClassToUpdate *classToUpdate;
- (void) createVariables;
- (void) update;
@end
执行:
#import "SubOtherClass.h"
@implementation SubOtherClass
- (BOOL) acceptsFirstResponder{
return true;
}
- (void) createVariables{
self.classToUpdate = [[ClassToUpdate alloc] init];
self.thingToRender1 = [[ThingToRender alloc] init];
self.thingToRender2 = [[ThingToRender alloc] init];
}
- (void) keyUp:(NSEvent *)theEvent{
[super keyUp:theEvent];
[self setNeedsDisplay:true];
}
- (void) keyDown:(NSEvent *)theEvent{
[super keyDown:theEvent];
}
- (void) update{
[self.classToUpdate update:self];
}
- (void) drawRect:(NSRect)rect{
[super drawRect:rect];
[self update];
[[NSColor blackColor] set];
NSRectFill(rect);
[self.color1 set]; //this color is declared in superclass
NSString *str1 = [NSString stringWithFormat:@"%d %d %d %d", self.thingToRender1.x, self.thingToRender1.y, 30, 100];
NSRectFill(NSRectFromString(str1));
[self.color2 set]; //this color is declared in superclass
str1 = [NSString stringWithFormat:@"%d %d %d %d", self.thingToRender2.x, self.thingToRender2.y, 30, 100];
NSRectFill(NSRectFromString(str1));
[self.color3 set]; //this color is declared in superclass
str1 = [NSString stringWithFormat:@"%d %d %d %d", self.classToUpdate.x, self.classToUpdate.y, 30, 30];
NSRectFill(NSRectFromString(str1));
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
[self setNeedsDisplay:YES];
return self;
}
@end
OtherClass 扩展 NSView