开发者

calling action obtained from button

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 invo开发者_开发技巧ke the method obtained from 2nd pt. in same way!


Try:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜