How can get a reference to an NSWindow created in a xib file? - objective-c

I use a xib file to show an NSWindow named mainWindow - now I want to get a reference to mainWindow via code (e.g. NSWindow *mainWindow). I can't find anything in the documentation, any pointers?

The xib file will have placeholder objects in it for the app delegate and / or the file's owner.
On the assumption that it has the app delegate in it, you can get a reference to the window or any object in the xib by
Declare a property in the app delegate of the right type and with IBOutlet as part of its type:
#property (weak) IBOutlet NSWindow* theWindow;
Locate the app delegate object in the xib. Click and drag it while the control key is pressed. You should get a line between the mouse pointer and the object.
Drag on to the window and release the mouse button.
You should see a list of the outlets in the app delegate. Select theWindow and your done.

Related

(OSX/Cocoa) How to set controller for main window

I've created a new cocoa application using .xib files (not storyboarded, the app has to be backwards compatible with mavericks/mountain lion) and I would like to have a custom windowcontroller for the main window. Is this possible? I can't seem to find a way to connect the window to my desired custom controller. The window has a reference outlet in AppDelegate, however I need a custom NSWindowController for this window as it doesn't open on application launch. The application launches silently as a menu bar app and the main application is launched via button press in the drop down from the menu bar.
Is there a way to link a controller to the window in the interface builder? Or do I have to do something along the lines of :
wc = [[CustomWindowController alloc] initWithWindowNibName:#"Main"];
Thanks!
Yes, open up Utilities (the right panel) in Interface Builder, and at the bottom click on the Object Library (circle with square in it).
Search for Object (blue cube), and drag it into your Document Outline (the panel on the left inside of interface builder)
From there, select that object you just created, and change the Class in the Identity Inspector to be the window controller you want.
Finally you can go into the Connections Inspector and connect your window to the window outlet
I can't seem to find a way to connect the window to my desired custom
controller. The window has a reference outlet in AppDelegate, however
I need a custom NSWindowController for this window as it doesn't open
on application launch.
Another way:
1) Delete the window in MainMenu.xib. Delete the window property in AppDelegate.m--because you deleted the window, it is no longer relevant.
2) File>New>File>Cocoa Class. Enter a class name, e.g. MainWindowController; select "Subclass of: NSWindowController"; check "Also create .xib file for user interface".
3) Create an outlet in AppDelegate.m:
#import "AppDelegate.h"
#import "MainWindowController.h"
#interface AppDelegate ()
#property (strong, nonatomic) MainWindowController* windowController;
#end
4) Declare an action in AppDelegate.h:
#interface AppDelegate : NSObject <NSApplicationDelegate>
-(IBAction)launchWindow:(id)sender;
#end
and implement it in AppDelegate.m:
- (void)launchWindow:(id)sender {
[self setWindowController:[[MainWindowController alloc]
initWithWindowNibName:#"MainWindowController"]];
[[self windowController] showWindow:nil];
}
5) In MainMenu.xib, hook up the Menu Item to the launchWindow() action: control drag from the Menu Item to the AppDelegate object and select launchWindow.
Create the controller and make it extend from NSWindowController.
In your xib file select the File's Owner and set it to your custom class.
Select your NSWindow and connect it to the File's Owner.
To open the window:
In your .h:
#property (strong, nonatomic) YourWindowController *yourWinController;
In your .m:
self.yourWinController = [[YourWindowController alloc] initWithWindowNibName:#"YourWindowController"];
[self.yourWinController showWindow: nil];

cocoa windowDidEnterFullScreen not being called

I'm writing a Cocoa application which should work in fullscreen mode. I would like to detect the user setting the application window to fullscreen mode.
In Xcode,
in the Attributes Inspector, Full Screen value is "Primary Window"
in the File Inspector, Use Auto Layout is unchecked
the view of the main window has Autoresizes Subviews unchecked
I'm making the AppDelegate an NSWindowDelegate like this in AppDelegate.h
#interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>
and have a method like this in AppDelegate.m
-(void) windowDidEnterFullScreen:(NSNotification *)notification
{
vuMain.view.frame = NSMakeRect(0, 0, window.frame.size.width,
window.frame.size.height);
NSLog(#"AppDelegate - windowDidEnterFullScreen");
}
But this method is not getting called as there is no NSLog output. A watch set in the method does not get reached.
What am I doing wrong? What am I missing?
Please help! I'm rather new to Cocoa and am struggling for one full day with this.
Thanks
You need to actually make the AppDelegate the window's delegate. You either have to connect the window's delegate outlet to the AppDelegate in the NIB, or you need to set the window's delegate property programmatically.

display NSMenuItems list

I created a simple mac OS application and placed NSMenu in the nib file then I create two other files MenuManager.h and MenuManager.m where I created NSMenuIBOutlet and menu items etc. In MenuManager I have a method called setup which creates NSStatusItem and set menu to NSMenu which have created in header file as #property (weak) IBOutputlet NSMenu *menu;
In AppDelegate nib file I have selected Menu and refererenced outlet to menu object in MenuManager.
In AppDelegate I am calling method setup on MenuManager object which creates status item in status bar with image etc but when I click on it then list doens't appear.
Any idea where I am wrong?

Window called with "showWindow" not focused

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?
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.

In iOS, how do I reference an object in a view that is created with a xib file?

I have a view controller that is instantiated in appDelegate and pushed onto a navigation controller. The view controller's view is created with a xib file. The xib file puts a UILabel on the view (among other things). Now I need to set the label's text property programatically but I don't see how to get a reference to it. How do I reference the label object?
I'm using xcode 3.2.5 and building an iPad app.
Aside from IBOutlets, you can also set a tag property on the label in the IB. Then, when you need it you can do:
UILabel *label = (UILabel *)[self.view viewWithTag:111];
111 of course being the tag you assigned to the label in IB.
You do this with what's called an "outlet". You define them in your controller, mark them clearly as IBOutlet and then connect them in Interface Builder to your file owner (or other delegate object created in IB).
For instance, in your FooController.m you might have this:
#interface FooController ()
#property (nonatomic, weak) IBOutlet UILabel* fooLabel;
#end
Then you would select your label, and either control drag from it to the file owner, or go to its connections tab, and drag from the + under referencing outlet, to the file owner and select the fooLabel.
UPDATE: Code sample changed to reflect modern way of handling this case.
[self.view viewWithTag:NUMBER_OF_TAG]; does the trick. But remember that if you want to access the view you must do it on the viewWillAppear or viewDidAppear events.