NSButton selector selector

I just want to add an NSButton with SetAction arguments.

NSRect frame = NSMakeRect(10, 40, 90, 40); NSButton* pushButton = [[NSButton alloc] initWithFrame: frame]; [pushButton setTarget:self]; [pushButton setAction:@selector(myAction:)]; 

But I want to put an argument to the myAction function ...
How?
Thanks.

+4
source share
3 answers

But I want to put an argument to the myAction function ...
How?

You can not.

... if there is more than one button using this method, we cannot differentiate the sender (only with a header) ...

There are three ways to tell which button (or other control) tells you:

  • Assign each button (or other control) a tag and compare the tags in your action method. When you create controls at the tip, this has the disadvantage that you need to write the tag twice (once in code, once in nib). Since you are writing a button manually from scratch, you do not have this problem.
  • You have an output for each control that you expect to send you this message, and compare the sender with each outlet.
  • They have different methods of action, and each control is the only one connected to each action. Then, each action method does not need to determine which control sent this message to you, because you already know what way it is.

The problem with tags is the aforementioned repeatability. It’s also very easy to neglect the name of each tag, so you end up looking at code like if ([sender tag] == 42) and don’t know / should look for which control is 42.

The problem with the exits is that your action method can take a very long time, and be that as it may, it probably performs several different actions that do not have a business in the same method. (This is also a problem with tags.)

So, I usually prefer the third solution. Create an action method for each button (or other control) that will have you as the target. Usually you call a method and a button the same (for example, save: and "Save") or something very similar (for example, terminate: and "Quit"), so you will know by simply reading every method that closes it belongs.

+4
source

I never programmed NSButton, but I think you just need to create a method like this:

 - (void) myAction: (NSButton*)button{ //your code } 

What is it!!

+1
source

All Articles