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

前端 未结 2 1720
眼角桃花
眼角桃花 2021-01-24 23:50

Any idea how to set button selector at runtime (on click event) for mac (Not IOS)

I have a view controller with a button declared as outlet :

#import <         


        
相关标签:
2条回答
  • 2021-01-25 00:19

    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.

    0 讨论(0)
  • 2021-01-25 00:20

    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);
    }
    
    0 讨论(0)
提交回复
热议问题