setting target and action for a NSSlider programmatically in a class that is not "file's owner" - how?

  • I'm trying to set the action for an NSSlider in a little helper class
    that is not the "file's owner" class associated with the window in the
    nib. At runtime, I get these console messages:

    *** +[SliderHelper myAction:]: unrecognized selector sent to class
    0xeb74380
    HIToolbox: ignoring exception '*** +[SliderHelper myAction:]:
    unrecognized selector sent to class 0xeb74380' that raised inside
    Carbon event dispatch

    This is how I'm setting this up:
      - there's an NSObject subclass (call it MyDialog) containing
    IBOutlets for all the controls I care about.
      - there's another NSObject subclass (call it SliderHelper) which is
    intended to automate some behaviour common to a few controls in the
    dialog (and other dialogs in future)
      - MyDialog instantiates a SliderHelper and passes it an NSSlider*
    outlet referring to a slider
      - SliderHelper sets up the target and action for the slider
    programmatically in an +initWithSlider: method..

    +(id) initTestSlider:(NSSlider*)aSlider {
    SliderHelper* newOne = [[SliderHelper alloc] init];
    [newOne setSlider:aSlider];
    [[newOne slider] setTarget:self];
    [[newOne slider] setAction:@selector(myAction:)];
    return newOne;
    }

      - myAction: is a void returning method with no params, and it is
    implemented!

    How can I make this work?

    Note that if I set the target and action from MyDialog (with a
    MyDialog -myAction of course) it works, what am I missing here?

    thanks
    Rua HM.
  • On Thu, May 15, 2008 at 10:12 PM, Rua Haszard Morris <
    <r.haszardmorris...> wrote:

    > I'm trying to set the action for an NSSlider in a little helper class that
    > is not the "file's owner" class associated with the window in the nib. At
    > runtime, I get these console messages:
    >
    > *** +[SliderHelper myAction:]: unrecognized selector sent to class
    > 0xeb74380
    > HIToolbox: ignoring exception '*** +[SliderHelper myAction:]: unrecognized
    > selector sent to class 0xeb74380' that raised inside Carbon event dispatch
    >
    > This is how I'm setting this up:
    > - there's an NSObject subclass (call it MyDialog) containing IBOutlets for
    > all the controls I care about.
    > - there's another NSObject subclass (call it SliderHelper) which is
    > intended to automate some behaviour common to a few controls in the dialog
    > (and other dialogs in future)
    > - MyDialog instantiates a SliderHelper and passes it an NSSlider* outlet
    > referring to a slider
    > - SliderHelper sets up the target and action for the slider
    > programmatically in an +initWithSlider: method..
    >
    > +(id) initTestSlider:(NSSlider*)aSlider {
    > SliderHelper* newOne = [[SliderHelper alloc] init];
    > [newOne setSlider:aSlider];
    > [[newOne slider] setTarget:self];

    -init* is always an instance method, and always called as part of a [[Foo
    alloc] initBlah] pair. Above, you're passing a Class object to setTarget,
    but targets must be instances - hence the "unrecognized selector" error when
    you try to send the action to the target.

    I would write it like this:

    -(id) initWithSlider:(NSSlider*)aSlider {
        if ((self = [super init])) {
            [aSlider setTarget:self];
            [aSlider setAction:@selection(myAction:)];

            [self setSlider:aSlider];
        }
        return self;
    }

    You should review the Cocoa Fundamentals Guide - especially the section
    about object creation:

        <
    http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals
    /CocoaObjects/chapter_3_section_6.html

    >

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
  • Aha!!

    Exactly, the self is not what I thought it was ... I understood the +
    was a class method, and kinda ignored the strange use of self.

    (I have only recently become aware that there is a 'self' in an
    instance method incidentally...)

    thanks for the help
    Rua HM.

    On May 16, 2008, at 2:38 PM, Sherm Pendley wrote:

    > On Thu, May 15, 2008 at 10:12 PM, Rua Haszard Morris <r.haszardmorris...>
    >> wrote:
    > I'm trying to set the action for an NSSlider in a little helper
    > class that is not the "file's owner" class associated with the
    > window in the nib. At runtime, I get these console messages:
    >
    > *** +[SliderHelper myAction:]: unrecognized selector sent to class
    > 0xeb74380
    > HIToolbox: ignoring exception '*** +[SliderHelper myAction:]:
    > unrecognized selector sent to class 0xeb74380' that raised inside
    > Carbon event dispatch
    >
    > This is how I'm setting this up:
    > - there's an NSObject subclass (call it MyDialog) containing
    > IBOutlets for all the controls I care about.
    > - there's another NSObject subclass (call it SliderHelper) which is
    > intended to automate some behaviour common to a few controls in the
    > dialog (and other dialogs in future)
    > - MyDialog instantiates a SliderHelper and passes it an NSSlider*
    > outlet referring to a slider
    > - SliderHelper sets up the target and action for the slider
    > programmatically in an +initWithSlider: method..
    >
    > +(id) initTestSlider:(NSSlider*)aSlider {
    > SliderHelper* newOne = [[SliderHelper alloc] init];
    > [newOne setSlider:aSlider];
    > [[newOne slider] setTarget:self];
    >
    > -init* is always an instance method, and always called as part of a
    > [[Foo alloc] initBlah] pair. Above, you're passing a Class object to
    > setTarget, but targets must be instances - hence the "unrecognized
    > selector" error when you try to send the action to the target.
    >
    > I would write it like this:
    >
    > -(id) initWithSlider:(NSSlider*)aSlider {
    > if ((self = [super init])) {
    > [aSlider setTarget:self];
    > [aSlider setAction:@selection(myAction:)];
    >
    > [self setSlider:aSlider];
    > }
    > return self;
    > }
    >
    > You should review the Cocoa Fundamentals Guide - especially the
    > section about object creation:
    >
    > <http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals
    /CocoaObjects/chapter_3_section_6.html
    > >
    >
    > sherm--
    >
    > --
    > Cocoa programming in Perl: http://camelbones.sourceforge.net
previous month may 2008 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