我试图添加一个NSRect时推按钮,但IBAction是在一个视图控制器,而绘图代码必须在视图中,由于某种原因我使用的代码赢得'牛逼得出这里的矩形是在视图控制器的操作方法来源:当按钮被按下时添加一个NSRect
-(IBAction)ButtonPressed:(id)sender {
NSRect Rect = NSMakeRect(10, 10, 100, 100);
NSColor* BlackFill = [NSColor blackColor];
[BlackFill set];
NSRectFill(Rect);
NSColor* whitestroke = [NSColor whiteColor];
[whitestroke set];
NSFrameRectWithWidth(Rect, 5.0);
[rects addObject:[NSValue valueWithRect:Rect]];
[self.rectView setNeedsDisplay:YES];
}
而对于在视图中绘制代码来源:
-(void)drawRect:(NSRect)dirtyRect {
//heres the part where I want to draw the NSRect but it does not work
if ([datasource conformsToProtocol:@protocol(MainViewDatasource)]) {
NSLog(@"DataSource conforms to protocol:MainViewDatasource");
NSUInteger numRects = [datasource numberOfRectsInView:self];
for (NSUInteger i = 0; i < numRects < 800; i++) {
NSRect currentRect = [datasource rectangleView:self rectAtIndex:i];
NSFrameRect(currentRect);
}
if (numRects >= 800) {
NSAlert* alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setMessageText:@"You have placed too many rectangle shapes in your level"];
[alert addButtonWithTitle:@"OK"];
[alert release];
}
}
/* The code here works great but has nothing to do with adding a rect when the button is pressed */
NSRect Rect = NSMakeRect(0.0, 0.0, 7000.0, 3500.0);
int width = Rect.size.width;
int height = Rect.size.height;
int i = 0;
[[NSColor blackColor] set];
NSBezierPath* drawingPath = [NSBezierPath bezierPath];
for (i=0; i<=width; i=i+GRIDSIZE) {
[drawingPath moveToPoint:NSMakePoint(i, 0)];
[drawingPath lineToPoint:NSMakePoint(i, height)];
}
for (i=0; i<=height; i=i+GRIDSIZE) {
[drawingPath moveToPoint:NSMakePoint(0, i)];
[drawingPath lineToPoint:NSMakePoint(width, i)];
}
[drawingPath stroke];
}
预先感谢您的帮助。
您是否收到“DataSource conforms ...”消息? – Chuck
不,那是我的另一个问题。 –
我已经在我的视图控制器中添加了代码给awakeFromNib方法,该方法应该设置DataSource以符合我的协议,但由于某种原因它什么都不做。 –