On Thu, Jun 12, 2008 at 10:26 AM, Vikas <[EMAIL PROTECTED]> wrote: > Hi, > > I have recently started programming on Mac using Objective-C and Cocoa. I am > coming from C++/C# world. So, its a fairly basic question. Please help me > understand the following code: > > @implementation MyView /*MyView inherits from NSView */ > -(void)drawRect: (NSRect)aRect { > [[NSColor blackColor] set]; > NSRectFill( [self bounds] ); > } > > In first line, I was expecting something like [self setColor:[NSColor > blackColor]]; (similar to this.color = NSColor.blackColor; in C#/C++) > how NSColor object knows about where to set the color?
When drawRect: is called the AppKit framework has "focused" the graphics context associated with the window (or some other type of context) that contains your view. The method -[NSColor set] interacts with the current graphics context to set the color you request. In other works the color of you drawing operations is not an attribute of your own object but the graphics context you are interacting with. > In second line, NSRectFill(), I was expecting it to be called using square > bracket [] notation. Again how this function knows where to fill the > rectangle? There is no reference of NSView passed into the function? NSRectFill is a utility function that you can use to fill a rectangle. You could also use NSBezierPath to do this. Again the target graphics context is already focused for you when AppKit calls drawRect:. Review... http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDrawingGuide/index.html http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaViewsGuide/index.html -Shawn _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]