0

I am trying to send an action from a menu item to a child view controller of a NSSplitViewController. As I understand I am supposed to override supplementalTargetForAction:sender: on the splitViewController to provide the child view controller to the responder chain (see this post).

However, supplementalTargetForAction:sender:never gets called.

The documentation states:

If this NSResponder instance does not itself respondsToSelector:, then supplementalTargetForAction:sender: is called.

In a debugging effort I have created a fresh Xcode project with just a normal viewController, overridden the method there to see if it gets called - it doesn't.

Here's my code:

@implementation ViewController

- (BOOL)respondsToSelector:(SEL)aSelector {
    NSString *s  = NSStringFromSelector(aSelector);
    if ([s hasPrefix:@"foo"]) {
        // let's pretend we do not respond here.
        // I expect `supplementalTargetForAction:sender:` to get called.
        return NO;
    }
    return [super respondsToSelector:aSelector];
}

- (id)supplementalTargetForAction:(SEL)action sender:(id)sender {
    // nobody calls me ever :(
    return [super supplementalTargetForAction:action sender:sender];
}

- (IBAction)foo:(id)sender {
    NSLog(@"Foo");
}

@end

storyboard sccreenshot

What am I doing wrong?

de.
  • 7,068
  • 3
  • 40
  • 69
  • 1
    The view controller isn't in the responder chain. The window is the first responder and sends `respondsToSelector:` to its `contentViewController`. Add a control which accepts first responder (editable text field) to the view. – Willeke Aug 07 '18 at 13:52
  • Thanks, that works for my test case and technically answers my question. However, in the real app, the VC with the action is an openGL view that sits inside the split view. So, there are no text fields/controls. So I probably need to override `acceptsFirstResponder` and return `YES`? – de. Aug 07 '18 at 15:06

0 Answers0