0

How can I change the functionality of a button that I used previously? For example, If I had a button that did "Proceed/Cancel" and let's say you "Proceed" the button would change to something such as "View/Go Back"? Basically I want to re-use the same button for something else, but since I don't know how maybe someone can help me understand it better. Thank you.

- (IBAction)someButton:(NSButton *)sender {

       if ([someString isEqualToString:someThing]) {
            isAllowed = YES;
            [oneButton setTitle:@"Proceed"];
            [self continue];
        }
        else {
            [oneButton setTitle:@"Cancel"];
            return;
            }
        }

- (void)continue {

// I would like to make someButton (above) take on different functionality 
// here if that's even possible. such as:

  [oneButton setTitle:@"View"];
  [self whatNow];
Joe Habadas
  • 628
  • 8
  • 21

1 Answers1

3

At some point in your program lifecycle you could replace the previous target and/or action of a NSButton by the desired one.

[oneButton setAction:@selector(continue)];

This will cause your continue selector to be called instead of the someButton: for the oneButton instance.

OBS: just pay attention at your selectors as the one from the NIB file has a parameter @selector(someButton:) and the one you are creating does not have any, so it stays as @selector(continue)

as seen here: Cocoa forControlEvents:WHATGOESHERE

Community
  • 1
  • 1
Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112
  • thank you - i'll give this a try. One more question I have; is it possible to have a button target two methods at the same time? – Joe Habadas Sep 09 '12 at 21:18
  • @JoeHabadas yes, sure! you can have many targets/action pairs for the same UIControlEvent, just keep in mind that they will be fired up in the same order they were added – Felipe Sabino Sep 09 '12 at 21:29
  • also, it is possible to remove al targets/actions at once: http://stackoverflow.com/questions/3340825/uibutton-remove-all-target-actions - I will add an edit for this :) – Felipe Sabino Sep 09 '12 at 21:53
  • Thanks very much Felipe. I'm trying to use this with Cocoa; do you know the Mac equivalent to UIControlEventAllEvents, etc? – Joe Habadas Sep 09 '12 at 22:07
  • @JoeHabadas hmm sorry, I completely ignored the `nsbutton` at the question and went for the uibutton methods – Felipe Sabino Sep 10 '12 at 01:09
  • just updated my answer and as far as I know, you won't be able and you don't need to do it as NSButton has only one target/action, and not a collection – Felipe Sabino Sep 10 '12 at 01:18