Reputation: 35
I found this example of drawing a rectangle in objective-c cocoa
NSRect r = NSMakeRect(10, 10, 50, 60);
NSBezierPath *bp = [NSBezierPath bezierPathWithRect:r];
NSColor *color = [NSColor blueColor];
[color set];
[bp stroke];
However, where should I specify which NSView to draw on in the code?
For example, if I have two NSView objects, and I run this code, how do I specify which one to draw on?
Upvotes: 3
Views: 868
Reputation: 21383
You don't run this code on an NSView
, rather it runs in an NSView
subclass's override of the -drawRect:
method.
You'll need to create your own subclass of NSView
, then in that subclass, override -drawRect:
and put this code there:
@interface CustomView : NSView
@end
@implementation CustomView
- (void)drawRect:(NSRect)dirtyRect
{
NSRect r = NSMakeRect(10, 10, 50, 60);
NSBezierPath *bp = [NSBezierPath bezierPathWithRect:r];
NSColor *color = [NSColor blueColor];
[color set];
[bp stroke];
}
@end
Finally, you will of course have to instantiate an instance of CustomView
and add it to your view hierarchy, just as you would normally do with a regular NSView
.
Upvotes: 4
Reputation: 31026
Subclass NSView
, add your code to the drawRect:
method of the subclass, and replace the appropriate NSView
with an instance of your class.
Upvotes: 1