Custom NSViews

  • Okay, I've got my custom NSView subclass to draw my Shape objects (or subclasses thereof). Each Shape inherits or overrides:

    -(BOOL)shapeContainsPoint:(CGPoint)thePoint;

      Now, I want to detect clicks within the view and iterate over all the objects the view is responsible for, and send them the above message. My Document.h declares:

    -(IBAction)didClick:(id)sender;

    which - for now - just does an NSLog(). How do I get my ShapeView class (the custom NSView subclass) to have the necessary code/declarations so that IB can see that it can originate an action, just like an NSButton or other control (i.e. Ctrl-drag from the control to the action in the delegate).
      Furthermore, what's the recommended way to communicate the X,Y position (CGPoint) where the user clicked in the drawable area of the view -> delegate that has the action triggered when the user clicks (i.e. see above)? Should the view hold on to the X,Y location, then provide these are readonly properties which the action code in the delegate can fetch through the (id)sender? Or is there a better way?
  • iOS or Mac OS?

    On Jul 6, 2012, at 7:55 PM, William Squires wrote:

    > Okay, I've got my custom NSView subclass to draw my Shape objects (or subclasses thereof). Each Shape inherits or overrides:
    >
    > -(BOOL)shapeContainsPoint:(CGPoint)thePoint;
    >
    > Now, I want to detect clicks within the view and iterate over all the objects the view is responsible for, and send them the above message. My Document.h declares:
    >
    > -(IBAction)didClick:(id)sender;
    >
    > which - for now - just does an NSLog(). How do I get my ShapeView class (the custom NSView subclass) to have the necessary code/declarations so that IB can see that it can originate an action, just like an NSButton or other control (i.e. Ctrl-drag from the control to the action in the delegate).
    > Furthermore, what's the recommended way to communicate the X,Y position (CGPoint) where the user clicked in the drawable area of the view -> delegate that has the action triggered when the user clicks (i.e. see above)? Should the view hold on to the X,Y location, then provide these are readonly properties which the action code in the delegate can fetch through the (id)sender? Or is there a better way?
  • On Jul 6, 2012, at 4:55 PM, William Squires wrote:

    > How do I get my ShapeView class (the custom NSView subclass) to have the necessary code/declarations so that IB can see that it can originate an action, just like an NSButton or other control (i.e. Ctrl-drag from the control to the action in the delegate).

    I think it would have to subclass NSControl; then IB would let you wire up its target/action properties. This is a bit of a dead-end, though, as it only lets you communicate one thing, i.e. a click on a shape. You're probably going to want something richer.

    Another way to do it is to create a 'delegate' or 'target' IBOutlet on your view class that you wire up to the object that will be called when events happen. If you want to be formal about it you can define a protocol in your view's header, and then have the document implement it. This way you can extend that protocol to add methods that will be called in other circumstances.

    —Jens
  • MacOS X (10.6 base SDK)

    On Jul 6, 2012, at 7:13 PM, Alex Zavatone wrote:

    > iOS or Mac OS?
    >
    > On Jul 6, 2012, at 7:55 PM, William Squires wrote:
    >
    >> Okay, I've got my custom NSView subclass to draw my Shape objects (or subclasses thereof). Each Shape inherits or overrides:
    >>
    >> -(BOOL)shapeContainsPoint:(CGPoint)thePoint;
    >>
    >> Now, I want to detect clicks within the view and iterate over all the objects the view is responsible for, and send them the above message. My Document.h declares:
    >>
    >> -(IBAction)didClick:(id)sender;
    >>
    >> which - for now - just does an NSLog(). How do I get my ShapeView class (the custom NSView subclass) to have the necessary code/declarations so that IB can see that it can originate an action, just like an NSButton or other control (i.e. Ctrl-drag from the control to the action in the delegate).
    >> Furthermore, what's the recommended way to communicate the X,Y position (CGPoint) where the user clicked in the drawable area of the view -> delegate that has the action triggered when the user clicks (i.e. see above)? Should the view hold on to the X,Y location, then provide these are readonly properties which the action code in the delegate can fetch through the (id)sender? Or is there a better way?
    >
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today