Я пытаюсь добавить NSRect, когда кнопка нажата, но IBAction находится в одном контроллере представления, в то время как код рисования должен быть в представлении, и по какой-то причине код, который я использую, не рисует прямоугольник здесь, является источникомдля метода действия в контроллере вида:
-(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];
}
Заранее благодарим за помощь.