I have a top level NSPanel in my MainMenu.xib. I specified the class
of the panel to be my class which subclasses NSPanel. I was expecting the
method initWithCoder: to be called when the application launched but it
is not.
Does anyone know what I need to do get initWithCoder: called from MainMenu.xib
when the application launches? I'm new to Xcode and the UI of the xib file
in xcode is a bit overwhelming.
Thanks.
According to the documentation:
Although the NSWindow class inherits the NSCoding protocol from
NSResponder, the class does not support coding.
Have you checked the other initializers?
-initWithContentRect:styleMask:backing:defer:
-initWithContentRect:styleMask:backing:defer:screen:
Related
Apple’s developer documentation on Nib Files in Interface Builder mentions the Application placeholder (highlighted in the picture above) but doesn’t explain its purpose or when it should be used. The article explains the other two placeholders—File’s Owner and First Responder—fairly well.
I would appreciate any information on the Application placeholder, with links to any documentation that I may have missed.
Also, in the Identity Inspector, why is the Application placeholder of type NSObject instead of, for example, NSApplication?
From the documentation of Interface Builder 3.2.6 (copyright 1999-2010):
In Cocoa nib files, the Application placeholder object gives you a way to connect the outlets of your application's shared NSApplication object to custom objects in your nib file. The default application object has outlets for its delegate object and, in Cocoa applications, the application menu bar. If you define a custom subclass of NSApplication, you can connect any additional outlets and actions defined in your subclass.
In an old MainMenu.nib file from 2009, the delegate is connected to the Application placeholder instead of the file's owner. In a XIB file from 2012, the Application placeholder isn't class NSObject. Nowadays the only use I can think of is binding something to Application.delegate.someProperty.
I'm trying to build a menubar application on OS X with the NSMenu being shown when the menubar icon is clicked having a custom amount of NSMenuItems. The amount is specified in a settings window and I thought the best way of carrying this number over would be to save it to NSUserDefaults and sending a NSNotificationCenter notification when the value has changed, so the controller in charge of setting up and holding the data for the NSMenu can load this value from the defaults when the notification is received.
The problem I'm experiencing here though is that I'm unsure how to tell the menu controller to subscribe to the notifications. Since I just subclassed NSMenu I don't really have an initializer where this can be done. Or an equivalent to a didLoad method that NSWindowControllers have.
Another option would be to maybe have the menu controller be a singleton and speak directly to that without going through the notification center. Or have a reference to it in the app delegate which would amount to the same thing here.
Or maybe I'm overthinking this entirely and there's an easier way of working with this?
Thanks for any help and tips!
Of course NSMenu has an initializer. All classes do.
Probably, you instantiated your menu in a NIB. In that case, loading the NIB will initialize the menu by calling -initWithCoder:, which NSMenu implements as part of adopting the NSCoding protocol.
If you're instantiating the menu in code, then you must be calling an initializer as part of that (you call [[NSMenu alloc] init...], where init... is some specific initializer).
Your scheme with notifications should work fine.
You could also have your app controller mediate between your settings window and the menu. It would have an outlet to the menu so it could call any appropriate methods, including ones added by your subclass.
I am trying to create a custom UIView for an iPhone app. The UIView contains some standards views such as UILabel and UIImageView.
I have created a new nib file and designed the control there. Afterwards I created a new class that extends UIView. I have put owner to the class, linked the controls from the nib to the class, implemented the methods in initWithCoder: etc.
Here is the catch: if in the storyboard I add a UIView object with the my UIView custom class, at build time everything works. But in the Interface Builder I still see a white rectangle.
I had a look on the Apple tutorial here (https://developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/chapters/CreatingaLiveViewofaCustomObject.html) and it works as stated if I implement drawRect method.
But the controls are not displayed, such as UILabel.
My question is if there is any way to see the custom control in the Interface Builder and how may I achieve this?
I have a new project I am working on in Xcode 5.1 and I have been using Interface Builder to create and bind most of my layouts. I customized the default MainMenu.xib to contain the menu items I want but I cannot figure out how to bind the selectors for each menu item.
The project has a MainWindow.xib and a MainWindowController that is used by my app delegate and the controller is where I would expect the menu selectors to go, but I don't know how to do that.
The drag-and-drop connections panel will not work with the controller.
The MainMenu.xib's owner is the NSApplication, which doesn't seem right for the selector.
How do (and where should) I handle the MainMenu.xib actions?
Note: my question is very similar to this one but since I have done everything in IB and the accepted answer is in obj-c, the answer is of no use to me.
In my own project, I have a MainMenu.xib and a MainWindow.xib file.
MainMenu.xib is the default starting xib file when one isn't using a storyboard for an app. That's why NSApplication lives in there, it needs to be instantiated.
My MainWindow.xib is owned by my own MainWindowController implementation.
For the various menu items in MainMenu.xib, I am pointing those items at my AppDelegate (which is the true file owner for MainMenu.xib), and with those IBActions I'm firing off methods in other objects, notifications to other objects, or instantiating objects to get whatever task done that's associated with that menu item.
If you have additional classes & objects instantiated within MainMenu.xib, you can add IBActions in those classes and connect the menu items directly to them.
I'm looking in to using NSWindowController and I just can't think how to get it working. How do I start using it?
It's difficult to answer this question without knowing what you're trying to do. However, if you are writing a document-based application, an NSWindowController is automatically created for each window you create, so you don't need to create one specially.
The way I use NSWindowController is I create a different subclass for each type of window in my application. For example, I might have a 3D application with an AppWireframeWindowController and an AppPreviewWindowController. Each subclass automatically loads the correct nib file, and has code that hooks the document's data to the views in the nib.
If you are using storyboards you can connect an NSWindowController subclass up in IB. Otherwise if you are using nibs and have just the default template for a Mac Cocoa app then you may need to make it in code or just use a subclass of NSWindow.
Otherwise you can create a new NSWindowController and check the 'Also create XIB file for user interface' and it will give you the nib and also the NSWindowController subclass. It is basically a new nib where 'File's Owner' is your NSWindowController and the Window is the .window object inside the NSWindowController and the delegate is also pointed there.
You may be able to modify that.