2

i have a simple cocoa coredata statusbar application with Xcode 4.6.2. This is the situation:

Renamed MainMenu.xib to PreferencesWindow.xib, deleted the mainmenu, created a simple and working coredata function with arraycontrollers and bindings in the window. I have created a new file->User Interface->Main Menu and named it StatusBarMenu.xib. Added a simple menu to it and removed the main menu. Created new file->objective-c class->subclass of NSObject and named it StatusBarController. Here's the code for the interface:

@property IBOutlet NSMenu *statusMenu;
@property NSStatusItem *statusItem;
@property [some items for statusbar image]

implementation:

@synthesize [everything]
-(void)awakeFromNib{
    statusItem = [[NSStatusBar systemStatusBar]statusItemWithLength:NSVariableStatusItemLength];
    [some string, path and stuff for the images]
    statusItem.menu = statusMenu;
    statusItem.toolTip = @"";
    statusItem.highlightMode = YES;
}

Then I've created another new file->objective-c class->subclass of NSWindowController, named it PreferencesWindowController and leave it as it is. Then a new file->objective-c class->subclass of NSObjects named PreferencesAppController. Here's the code for .h:

@property (assign) IBOutlet NSWindow *mainWindow;
@property (retain) PreferencesWindowController *prefController;
-(IBAction)showPreferences:(id)sender;

.m code:

@synthesize [everything];
-(IBAction)showPreferences:(id)sender{
    if(!self.prefController)
        self.prefController = [[PreferencesWindowController alloc] initWithWindowNibName:@"PreferencesWindow"];

    [self.prefController showWindow:sender];
}

In the AppDelegate files there's only code for coredata, nothing added. Then in the PreferencesWindow.xib I've added NSObject (the blue cube) for PreferencesAppController with some bindings: Outlets-> mainWindow binded to the window with the simple coredata function. AppDelegate has the window outlet binded to the same window, then Referencing Outlets->File's Owner delegate, some saveaction and managedobjectcontext. In the StatusBarMenu.xib i've created a StatusBarController object and binded it to the menu (outlets->statusMenu), created another blue object called PreferencesAppController with Received Actions->showPreferences binded to a menu item.

Then i run the program and everything goes fine: an icon appears in the status bar, the dropdown menu works, if i click on "preferences..." the preferences window appears but... it isn't focused! It's on top of the other windows but i have to click to make it focused. The coredata saving functions works fine except that i have to manually save with a button, quitting the application from the statusbar menu does not save, but this is a marginal issue.

Why isn't the window focused?

4

1 回答 1

2

我从您将您的应用程序描述为“状态栏应用程序”的描述中假设它旨在在后台运行而不显示在 Dock 中。

这意味着您的应用程序不是活动应用程序。用户单击您的状态项并从其菜单中选择一项不会改变这一点。

当不是活动应用程序的应用程序打开一个窗口时,该窗口不会获得焦点(因为这通常相当于从用户在活动应用程序中所做的任何事情中窃取焦点

因此,您需要激活您的应用程序

于 2013-06-13T05:16:32.493 回答