Force NSPopover to not become first responder with NSTokenField

I'm having a NSTokenField and I'm providing a custom autocompletion inside the popover. Unfortunately as soon as the popover is shown it forces the NSTokenField to resign first responder. Is there a way to show the popover but not to loose the first responder on the NSTokenField?

Overriding NSTokenField's resignFirstResponder causes the NSTokenField to stop working (it doesn't accept any keystrokes). Overriding NSPopover's acceptsFirstResponder method or setting it's behaviour as suggested in this question does not work either.

Edit: The NSTokenField is not inside the NSPopover's contentViewController.view. Edit2: The behaviour like the search bar in the Mailapp would be to optimal solution.

Unfortunately, there’s no clean way to do this. Luckily, though, I’ve done it the ugly way in Delicious Library 3—you need to put this method in a subclass of NSWindow, and make sure the document window in question is that subclass:

- (BOOL)makeFirstResponder:(NSResponder *)responder;
{
    // Prevent popover content view from forcing our current first responder to resign
    if (responder != self.firstResponder && [responder isKindOfClass:[NSView class]]) {
        NSWindow *const newFirstResponderWindow = ((NSView *)responder).window;
        NSWindow *currentFirstResponderWindow;

        NSResponder *const currentFirstResponder = self.firstResponder;
        if ([currentFirstResponder isKindOfClass:[NSWindow class]])
            currentFirstResponderWindow = (id)currentFirstResponder;
        else if ([currentFirstResponder isKindOfClass:[NSView class]])
            currentFirstResponderWindow = ((NSView *)currentFirstResponder).window;

        // Prevent some view in popover from stealing our first responder, but allow the user to explicitly activate it with a click on the popover.
        // Note that the current first responder may be in a child window, if it's a control in the "thick titlebar" area and we're currently full-screen.
        if (newFirstResponderWindow != self && newFirstResponderWindow != currentFirstResponderWindow && self.currentEvent.window != newFirstResponderWindow)
            for (NSView *responderView = (id)responder; responderView; responderView = responderView.superview)
                if ([responderView conformsToProtocol:@protocol(LIPopoverFirstResponderStealingSuppression)] &&
                    ((id <LIPopoverFirstResponderStealingSuppression>)responderView).suppressFirstResponderWhenPopoverShows)
                    return NO;
    }

    return [super makeFirstResponder:responder];
}

Now make sure the popover’s content view subclass implements this protocol:

// NSPopover doesn't respect -acceptsFirstResponder of its content view (Radar 10666891).
@protocol LIPopoverFirstResponderStealingSuppression <NSObject>
@property (readonly, nonatomic) BOOL suppressFirstResponderWhenPopoverShows;
@end

Please also file a bug with Apple to request NSPopover respect -acceptsFirstResponder of its content view; it is 100% the case that when multiple developers file bugs they get fixed.

osx Force NSPopover to not become first responder with , osx Force NSPopover to not become first responder with NSTokenField? Make a subclass of your popover content (a text view?) and implement� Responder by lgandx - Responder is a LLMNR, NBT-NS and MDNS poisoner, with built-in HTTP/SMB/MSSQL/FTP/LDAP rogue authentication server supporting NTLMv1/NTLMv2/LMv2, Extended Security NTLMSSP and Basic HTTP authentication.

Make a subclass of your popover content (a text view?) and implement -(void)canBecomeKeyView. Return NO there. It's called only once when the popover is shown, so you can still interact with it, but it doesn't steal the first responder status anymore.

Questions tagged [nspopover] - 绵阳斯懿新材料科技有限公司, Unfortunately as soon as the popover is shown it forces the NSTokenField to resign first responder. Is there a way to show the popover but not to loose the first � Become first responder without animation 2012-08-11 19:29:52 3; Cocoa-Touch - Make a read-only UITextField become first responder 2012-10-08 21:21:35 0; UITableViewCell won't become first responder (Showing UIMenuController on cell gesture) 2012-12-03 08:28:22 0

I adapted Will's answer for Swift:

protocol PopoverFirstResponderStealingSuppression {
    var suppressFirstResponderWhenPopoverShows: Bool { get }
}

class TTWindow: NSWindow {
    override func makeFirstResponder(_ responder: NSResponder?) -> Bool {
        if responder != firstResponder, let responderView = responder as? NSView {
            // Prevent popover content view from forcing our current first responder to resign

            let newFirstResponderWindow = responderView.window!
            var currentFirstResponderWindow: NSWindow? = nil

            let currentFirstResponder = firstResponder
            if let currentFirstResponder = currentFirstResponder as? NSWindow {
                currentFirstResponderWindow = currentFirstResponder
            }
            else if let currentFirstResponder = currentFirstResponder as? NSView {
                currentFirstResponderWindow = currentFirstResponder.window
            }

            // Prevent some view in popover from stealing our first responder, but allow the user to explicitly activate it with a click on the popover.
            // Note that the current first responder may be in a child window, if it's a control in the "thick titlebar" area and we're currently full-screen.

            if newFirstResponderWindow != self, newFirstResponderWindow != currentFirstResponderWindow, currentEvent?.window != newFirstResponderWindow {

                var currentView: NSView? = responderView
                while currentView != nil {
                    if let currentView = currentView as? PopoverFirstResponderStealingSuppression, currentView.suppressFirstResponderWhenPopoverShows {
                        return false
                    }

                    currentView = currentView?.superview
                }
            }
        }

        return super.makeFirstResponder(responder)
    }
}

NSTextField first responder, Force NSPopover to not become first responder with NSTokenField. I'm having a NSTokenField and I'm providing a custom autocompletion inside the popover. AKForm is a form framework for iOS 7 that aims to take care of all the mundane steps we usually repeat to create them. It will take care of the creation, styling and validation of fields by abstracting out all the boring stuff like table views and cells.

I happened to stumble upon this problem while designing a custom control to be used within a table view.

It seems that at least in recent versions of Mac OS X, you can send the table inside your Popover (or whatever focus-stealing view you've got there) a tableView.refusesFirstResponder = true.

The popover will no longer attempt to steal first responder if you do this.

macos, becomeFirstResponder(). TextField This is not the correct method to use. is the first text field in the window, it should be made first responder automatically. osx - Force NSPopover to not become first responder with NSTokenField - Stack Overflow; parsing - Q: log4net file parse with Regex NXLog;

Flag both Twitter and Facebook using Select2 plugin, 不幸的是,一旦弹出窗口显示它就迫使NSTokenField辞职第一响应者。 Force NSPopover to not become first responder with NSTokenField.

Responder by lgandx, So the problem is not select2, but the surrounding text input. Since regular if statement. Have a reserve for touch devices so they can get to the first menu item �

Cocoa: Avoiding 'Updates Continuously' in control binds, Responder by lgandx - Responder is a LLMNR, NBT-NS and MDNS Force NSPopover to not become first responder with NSTokenField�

Comments
  • Presumably the "view" loaded into the NSPopover is configured from a NIB file and loaded via a view controller? Is the first responder set-up correctly in that NIB file? AFAIK, the popover should honour the usual first responder behaviour of windows and views.
  • @tojanfoe I'm sorry the question could be understood the wrong way. The NSTokenField is not inside the popover. I've edited the question and added an image for clarity.
  • Perhaps you need to use something other than NSPopover. The expected behaviour of a popover is to present some controls with the user can interact with.
  • Yes, inside the popover some autocompletion suggestions will be placed. Actually I just want to copy the behaviour of iTune's top right search bar.
  • Then perhaps a menu is more appropriate as that also provides the target/action functionality required to complete the, err, auto-completion.
  • You're a lifesaver. I took a look at your app and it looks great. exactly what I'm trying to reproduce. Kind of hard to work with OS X when you come from the iOS playground ;)
  • OS X has been the ugly step kid for a couple years, it’s really starting to show. But I still love it.
  • Thanks for this great answer, it helps me a lot!
  • I can't get this to work. Through logging I discovered that only the NSTableView inside the popover is getting asked to be first responder, so I subclassed NSTableView and implemented the ResponderStealingSuppression protocol method. It is being called, and returning YES, but the popover still gets first responder status. Where might I be going wrong?
  • Doesn't work with nested popovers. Seems this code confuses the default handling as I'm getting: [Error] Exception reported by the runtime: NSWindow: -_newFirstResponderAfterResigining is not a valid message outside of a responder's implementation of -resignFirstResponder. in the call to [super makeFirstResponder: responder] after a nested popover has been shown and I click somewhere inside or outside the main popover.
  • This doesn't work. Tried it with an NSTextField in an NSPopover and the popover still steals first responder.