Question

I have following requirements:

  1. Obtain action associated with NSButton using : - (SEL)action
  2. Call the obtained action.

Can we perform 2nd pt. Generally we invoke an action like this- [self abc:nil] just thinking if we can invoke the method obtained from 2nd pt. in same way!

Was it helpful?

Solution

Try:

SEL actionSelector = [button action];
[self performSelector: actionSelector withObject:nil];

OTHER TIPS

The action is just a selector—the name of a method. Any number of objects may have a method by that name, and even if only one class implements the method, you may have any number of instances of that class. So, you can't just call the name of a method, because that doesn't express what object will respond to it. You need an object that implements that method, and you need to send that message by that name to that object.

The most likely object you want to send the action message to is the button's target, so get that, the same way you got its action, and send the message to that object. Or, better yet, send the button a performClick: message; if you want to simulate the user clicking the button, that's the way to do that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top