我有几行在两个不同的子类中复制的绘图代码。当我将这个绘图代码移动到它自己的类中,然后从drawRect
中调用它时:它被调用,但它永远不会绘制到屏幕上。什么是正确的方法防止在两个不同的drawRect
:方法中重复代码?如何防止2 drawRect:方法内的重复代码?
详情:我正在通过继承NSTableView
和NSTableCellView
进行自定义控制。我的绘图代码需要在drawRect
:这两个子类中。
我创建了一个声明一个方法的NSObject的子类。这里是执行:
@implementation TNLChartDrawingExtras
- (void)drawDividersInRect:(NSRect)rect startingAtDate:(NSDate *)startDate withZoomFactor:(NSNumber *)zoomFactor {
float pos = 0;
NSDate *currentDate = [startDate copy];
while (pos < rect.size.width) {
//draw the vertical divider
NSBezierPath *linePath = [NSBezierPath bezierPathWithRect:NSMakeRect(pos, 0.0, 1.0, rect.size.height)];
[[NSColor colorWithCalibratedWhite:0.85 alpha:0.5] set];
[linePath fill];
//increment the values for the next day
currentDate = [NSDate dateWithTimeInterval:86400 sinceDate:currentDate]; // add one day to the current date
pos = pos + (86400.0/ [zoomFactor floatValue]);
}
}
在我的NSTableView子类中,我为这个对象定义一个属性。然后在awakeFromNib
我创建这个类的一个实例:
- (void)awakeFromNib {
self.extras = [[TNLChartDrawingExtras alloc] init];
}
在drawRect:
我发这条短信:
- (void)drawRect:(NSRect)dirtyRect {
// more code here...
[self.extras drawDividersInRect:viewBounds startingAtDate:chart.startDate withZoomFactor:self.zoomFactor];
}
被执行的代码,但该行是应该汲取不会出现。如果我将drawDividersInRect:...
的代码放在drawRect:
方法中,它可以正常工作。
您应该添加一些代码,以便我们可以看到您正在尝试的内容。 – Matt
你检查过'self.extras'不是'nil'吗?这将解释它。 – djromero