-1

I have an NSTextField that repopulates itself based on the outcome of a result; I would like to automatically perform an IBAction (which is normally controlled by clicking a NSButton in the app). How could I perform the click, or IBAction automatically based upon the result?

appDelegate.h

@interface main : NSView <NSApplicationDelegate>
{
NSView *main;
IBOutlet NSTextField *textValue;
IBOutlet NSWindow* window1;
IBOutlet NSWindow* window2;
}
- (void)textUpdated;
- (IBAction)switchWindow:(id)sender;

Which I would like to trigger or bind to the (IBAction)switchWindow:

appDelegate.m

- (void)textUpdated
{
[textValue setStringValue:[NSString stringWithFormat:@"%@, Switching Window", stringValue]];
[self switchWindow:sender];
}

- (IBAction)switchWindow:(id)sender {
    [self performSelector:[NSApp keyWindow]==window?@selector(openWindow):@selector(closeWindow) withObject:nil afterDelay:0.0];
}

- (void)openWindow {
    [window1 showWindow:window2 open:YES];
}

- (void)closeWindow {
    [window2 showWindow:window1 open:NO];
}
Joe Habadas
  • 628
  • 8
  • 21
  • http://stackoverflow.com/questions/4028734/how-to-programmatically-fake-a-touch-event-to-a-uibutton Does this help? – MCKapur Sep 17 '12 at 02:33
  • 1
    @NULL, it might if it was for iOS — this is for Mac OS X (Cocoa); thanks – Joe Habadas Sep 17 '12 at 02:34
  • You want `doSomething:` to be called from `somethingVoid`? Just call it. It's a method like any other. – jscs Sep 17 '12 at 02:41
  • @JoshCaswell, it seems to have no effect — when used like `[self doSomething:nsview];` any thoughts? – Joe Habadas Sep 17 '12 at 02:44
  • 1
    Please post your actual code rather than made up examples, and explain what you're really trying to accomplish. – Caleb Sep 17 '12 at 03:45
  • @Caleb, okay — now what? – Joe Habadas Sep 17 '12 at 04:48
  • @JoshCaswell, I updated the question — please take a look – Joe Habadas Sep 17 '12 at 04:49
  • Try making `self` the parameter for `withObject` when you call `performSelector`. I suspect it's just dispatching the call to a nil object (perfectly legal in Obj-C, it just does nothing). – Xono Sep 17 '12 at 04:54
  • @Xono, unfortunately it still doesn't do anything :-( – Joe Habadas Sep 17 '12 at 05:01
  • I know this is probably a stupid question, but are you certain window1 and window2 aren't nil? As best I can tell, there's nothing wrong with the call. – Xono Sep 17 '12 at 05:08
  • What's `window`? I see `window1` and `window2`. – jscs Sep 17 '12 at 05:19
  • window1 and window2 are two different windows. The IBAction works just fine when I access it with a NSButton from IB — but not when trying directly. I have no idea why it's not working. – Joe Habadas Sep 17 '12 at 05:25

1 Answers1

1

Switching to real code improves the question, but it's still hard to understand exactly what you're asking. As Josh Caswell pointed out, an action is just a method and you're free to call it yourself. You seem to be doing that already in -textUpdated, but your assertion that "it still doesn't do anything" doesn't give us much to go on. So:

  • Does -textUpdated get called? If no, there's your problem.

  • Does -switchWindow: get called? Stick a breakpoint in that method and see if you stop there. (Hint: If you answered 'yes' to the first question, you'll stop there.)

  • What do you expect to happen that isn't happening?

  • Are the values of the various variables (ivars and local variables) what you expect?

  • Do -openWindow and -closeWindow get called appropriately?

  • What does [NSApp keyWindow] return? Does it ever return a pointer equivalent to window? (And by the way, it wouldn't hurt to tell us what window means here.)

  • Did you perhaps intend to write something like (note the '1' after 'window'):

    [self performSelector:[NSApp keyWindow] == window1 ? @selector(openWindow) : @selector(closeWindow) withObject:nil afterDelay:0.0];

  • Your code might be easier to understand if you just do one thing at a time. Yes, it's more lines, but it's a lot easier to debug:

    NSWindow *keyWindow = [NSApp keyWindow];
    if (keyWindow == window1) {
        [self openWindow];
    }
    else if (keyWindow == window2) {
        [self closeWindow];
    }
    
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Everything gets called as I move down your checklist. When I access switchWindow from an NSButton it changes from window1 to window2 without any issues; I'm at a loss here. – Joe Habadas Sep 17 '12 at 05:53
  • Maybe if I tried it similar to: http://stackoverflow.com/questions/4028734/how-to-programmatically-fake-a-touch-event-to-a-uibutton?lq=1 it might work? although that method is for iOS, so I'm not sure how to implement it for Cocoa; just a thought. – Joe Habadas Sep 17 '12 at 05:55
  • Best guess: part of what happens when you click the button is that the window containing the button becomes the key window; for whatever reason, when you call `-textUpdated`, the window is not the key window. Other than that, not enough info to go on. Just remember that magic isn't involved: there's a reason behind every bug, and your job is to find that reason. – Caleb Sep 17 '12 at 06:44
  • Thanks, I'll keep looking for the possible reason; Should I mark your answer as correct? I'm not sure if anyone is going to respond with anything else that might shed light on this. Also, on an unrelated note: why do I get down-voted when I ask a question, isn't that what this site is for? Seems like it defeats the purpose really. In fact it's discouraging; I wouldn't be asking the question for no apparent reason, and you probably wouldn't be taking the time to respond if it wasn't worth your time. – Joe Habadas Sep 17 '12 at 07:03
  • Don't mark an answer as the solution unless it solves your problem (but feel free to vote up any answers that are helpful). People vote questions down for any of several reasons: too vague, OP obviously made no effort to find the answer, off topic, etc. Don't let a few downvotes discourage you -- just try to write questions that include all the relevant details. – Caleb Sep 17 '12 at 13:49