Question

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?

Était-ce utile?

La solution

I'm assuming from your description of your app as a “statusbar application” that it is meant to run in the background and not show up in the Dock.

This means that your application is not the active application. The user clicking on your status item and choosing an item from its menu does not change that.

When an application that is not the active application opens a window, that window does not take focus (since this ordinarily would amount to stealing focus from whatever the user has been doing in the application that is active).

So, you need to activate your application.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top