The Bar Bar menu never gets reactivated.

I am creating a Mac application that is located only in the menu bar without a docking station item, and not with a key window, and there is no main menu (it LSUIElementis set to info.plist in it YES). When I first start the application, it applicationDidBecomeActive:is called as I expect. However, when another application receives focus, it applicationDidBecomeActive:is never called again.

This prevents the text box that I had in my application from becoming the first responder. When I first open the application, the text box is edited:

before another app gains focus

But after another application comes to the fore, the text field is not edited:

after another app gains focus

What I tried:

, NSMenu. :

[NSApp unhide];
[NSApp arrangeInFront:self];
[NSApp activateIgnoringOtherApps:YES];
[NSApp requestUserAttention:NSCriticalRequest];
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
[[NSRunningApplication currentApplication] unhide];

, , , . , . .

+5
3

, , runloop, NSMenu, . NSStatusItem, :

- (void)toggleMenu:(id)sender
{
  // App might already be active
  if ([NSApp isActive]) {
    [self.statusItem popUpStatusItemMenu:self.menu];
  } else {
    [NSApp activateIgnoringOtherApps:YES];
  }
}

- (void)applicationDidBecomeActive:(NSNotification *)notification
{
  [self.statusItem popUpStatusItemMenu:self.menu];
}

, , .

+2

, -becomeFirstResponder, , -canBecomeFirstResponder get .

, , / , , , , , .

, .

+1

-makeFirstResponder: . NSWindow NSResponder.

- (void)menuWillOpen:(NSMenu *)menu {
    [[NSApp mainWindow] makeFirstResponder:yourTextInputField];
}

, , , . , , -acceptsFirstResponder: YES

- (BOOL)acceptsFirstResponder {
    return YES;
}

Edit: Ah, look that you do not have a key window. It appears that NSMenu actually has a window associated with it, and it is safe to call -makeFirstResponder:. In some discussion here, it is suggested to override -viewDidMoveToWindow:in your view containing your text box in NSMenu, for example:

- (void)viewDidMoveToWindow {
    [super viewDidMoveToWindow];

    [[self window] makeFirstResponder:yourTextInputField];
}
+1
source

All Articles