Handling mouse click event on a NSButton

  • Hello,

    I have a NSButton and I would like to have some code executed when the
    button is clicked. Special circunstances for this is that my button is
    created purely in code, there are no nib files on the project and also
    no use of the Interface Builder, and nor can it be, because I'm
    developing a cross-platform library. I haven't found how to do that in
    those special circunstances.

    thank you very much,
    --
    Felipe Monteiro de Carvalho
  • > I have a NSButton and I would like to have some code executed when the
    > button is clicked. Special circunstances for this is that my button is
    > created purely in code, there are no nib files on the project and also
    > no use of the Interface Builder, and nor can it be, because I'm
    > developing a cross-platform library. I haven't found how to do that in
    > those special circunstances.

      This is covered in the introductory Cocoa material. See:

    http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals
    /CommunicatingWithObjects/chapter_6_section_5.html


      The target/action mechanism works the same in code as it does in
    IB. It's just that in code, you have to explicitly set the target/
    action.

    --
    I.S.
  • Hi, thanks, now I read those docs, but there is still something I
    don't understand. There they recommend I should use something like:

    [aControl setAction:@selector(deleteRecord:)];

    But setAction expectes just a selector as parameter, so it seams to me
    that a lot of information about the method to be called is missing.
    AFAIK a selector just identifies the name of the method, but the class
    to which it bellongs to is missing and also the pointer to it's
    instance and the pointer to the method code.

    I'm using Cocoa from Pascal using PasCocoa (
    http://wiki.lazarus.freepascal.org/PasCocoa ), so I can't use
    objective-c language constructions and need to get this working with
    just objective-c runtime headers. I tryed to see the assembly for
    @selector(something:); but it isn't promissing. It calls a objc_meth_
    something that I can't find anywhere.

    thanks,
    --
    Felipe Monteiro de Carvalho
  • > But setAction expectes just a selector as parameter, so it seams to me
    > that a lot of information about the method to be called is missing.
    > AFAIK a selector just identifies the name of the method, but the class
    > to which it bellongs to is missing and also the pointer to it's
    > instance and the pointer to the method code.

      Read the documentation again. Pay particular attention to the
    "target" concepts in addition to the "action" concepts.

    --
    I.S.
  • > I'm using Cocoa from Pascal using PasCocoa (
    > http://wiki.lazarus.freepascal.org/PasCocoa ), so I can't use
    > objective-c language constructions and need to get this working with
    > just objective-c runtime headers. I tryed to see the assembly for
    > @selector(something:); but it isn't promissing. It calls a objc_meth_
    > something that I can't find anywhere.

      I apologize - I completely missed this part. I see the syntax is
    something like TextField.setStringValue(...); ... I'm not sure how
    that would work with a method that expects a selector. That's a
    question that's probably best posed to the PasCocoa forums.

    --
    I.S.
previous month march 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