PDA

View Full Version : How to draw line in NSView?




Sergio10
Apr 9, 2009, 04:30 PM
Hi,

Sorry, for stupid question. I can't to draw line in NSView control.
Requirements are next:
- I have NSView and NSButton controls on the window.
- When I click button line should draw in the NSView control.

What should I type in my pushButtonAction ?

I allready typed:

- (IBAction) pushButtonAction: (id)sender {

NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
}


I use Xcode 3.1 and Leopard 10.5.6
Thanks in advance.



HiRez
Apr 9, 2009, 05:21 PM
First lock the view before drawing into it: [myView lock]. Then use NSBezierPath to describe the line (see NSBezierPath docs for this) and then [myPath stroke] method to draw the line into the view. Finally, unlock the view when done: [myView unlock]. You shouldn't need to get the context for this, when you lock the view it'll be the current context. There are other ways of doing it (using Quartz for example), but this is probably the most Cocoa-like way.

Sergio10
Apr 9, 2009, 05:33 PM
Ok.
I can't understood how to get pointer to my View for lock, unlock etc :(

HiRez
Apr 9, 2009, 06:26 PM
Ok.
I can't understood how to get pointer to my View for lock, unlock etc :(You need to declare an outlet for the view, probably in your application controller object's .h file, something like:

IBOutlet NSView *myView;

IBOutlet is a special keyword that tells Xcode that you intend to link this object to something in Interface Builder. Once you have done that, in Interface Builder, Control-drag from your app controller object to the view and link it to "myView", or whatever you're calling it. Now when you specify myView (or whatever) in your code, it'll refer to the view you drew and linked to in IB.

kainjow
Apr 10, 2009, 11:36 AM
I would suggest these two tutorials to get started with drawing:

http://cocoadevcentral.com/d/intro_to_quartz/
http://cocoadevcentral.com/d/intro_to_quartz_two/