1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

How to draw line in NSView?

Discussion in 'Mac Programming' started by Sergio10, Apr 9, 2009.

  1. macrumors regular


    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:
    I use Xcode 3.1 and Leopard 10.5.6
    Thanks in advance.
  2. macrumors 601


    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.
  3. macrumors regular

    I can't understood how to get pointer to my View for lock, unlock etc :(
  4. macrumors 601


    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.
  5. Moderator emeritus



Share This Page