Set Button selector (on click event) at runtime ? (Cocoa : Mac Project)

删除回忆录丶 提交于 2019-12-02 04:14:06
rano

You shall look at the NSControl class reference (from which NSButton inherits) for two separate methods setTarget: and setAction:.

You can do something like this in your code:

[MyBtn setTarget:self];
[MyBtn setAction:@selector(doStuff)];

take a look also at this answer.

those two methods are what you are looking for.

  • (void)setTarget:(id)target;
  • (void)setSelector:(SEL)selector;

================

[myButton setSelector:@selector(myButtonClickEvent:)];


-(void) myButtonClickEvent:(id) sender{
       NSLog(@"button:%@ be clicked. :)", sender);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!