Adding options to NSPopUpButton in Interface Builder - objective-c

So when I add an NSPopUpButton in Xcode's Interface builder, it automatically has 3 options in the dropdown which can be worked with in the IB; however, I want to have 8 options in my popup. I know I can just add these in code, but it would be much more convenient if there was a way to add them right in the IB, without making the popup button a property. Is there any way to do this?

If you expand the NSPopupButton, there is a "Popup Button Cell". Expand that and you will find a "Menu". You can drag new NSMenuItems to that menu in Interface Builder.

Related

NSMenu click in OS X

How can I detect when the menu in menu bar is clicked? I mean when whatever is clicked like for example "file" menu not a concrete position in the menu.
I tried to connect IBActions from menu items in interface builder from my code, but it does not work from the main menu item like "File" only from the submenus within this menu
First of all, what are you trying to achieve in the big picture? Perhaps knowing when the menu bar has been clicked is not the best approach.
You might be able to achieve what you're interested in by observing the NSMenuDidBeginTrackingNotification notification, but it's hard to tell. For example, this will fire for keyboard-initiated tracking, too.
Go to MainMenu.xib and look at the menu layout. It will be something like
Main Menu
Menu Item - File
Menu - File
Right click on The NSMenu you want to observe, not the NSMenuItem. attach the delegate to an object or controller of your choosing.
Then implement
- (void)menuWillOpen:(NSMenu *)menu

use UIMenuController with text

So i would like to use the UIMenuController to display text. i would like to add in help buttons (press a little button and something will pop up describing what something does) that would display either a UIMenuController or something that looks similar to it. it would just display a few lines of text and dismiss when pressed.
it kinda looks like UIMenuController is only meant for button selection. i could just use one button and set the title to the message i wish to display, but am doubtful on how well that would work. are there any other options available?
A UIMenuController takes it content from its menuItems property and the documentation says :
The default value is nil (no custom menu items). Each menu item is an
instance of the UIMenuItem class. You may create your own menu items,
each with its own title and action selector, and add them to the
editing menu through this property. Custom items appear in the menu
after any system menu items.
So you are obliged to use UIMenuItem objects as content for your UIMenuController. UIMenuItem only inherits from NSObject, and stores only a title and an action, both required. This make you unable to use any other kind of data as UIMenuItem.
As it is not an UIView, you can give it a UIButton or a UIImageView.
You could think to override UIMenuController but again it is a direct child of NSObject, so it doesn't have any behavior for customization.
The only solution left to you, is to rewrite your own UIMenuController, deriving it from UIToolbar for example. This would give you more or less the same look, you just have to customize the arrow and the round corner.

Using NSWindowController to manipulate a window

I have the following code:
AXWindowController *controller = [[AXWindowController alloc]
initWithWindowNibName:#"ActivateWindow"];
[controller showWindow:nil];
How do I use controller to make changes to the window ActivateWindow (eg. changing text on the window, etc.)? Step by step instructions would be appreciated. Thanks!
Well, none of the actual window elements are handled in your piece of code, your code just initializes and shows the window. (I've just had a quick scan through, so I'm assuming that it works)
If you want to, for example, display text in a window, the simplest way is to use an NSTextField.
Very simple instructions:
Drag and drop a Label item from the Interface Builder library into your window.
Drag and drop a button item from the Interface Builder library into your window.
In XCode, in your window controller, create an IBOutlet for your label, e.g. messageLabel
In XCode, in your window controller, create an IBAction for your label, e.g. changeLabel
In Interface Builder, drag and drop an "object" into your document. (Shortcut = CMD + 0)
Under the Identity tab, change the class to AXWindowController. (Shortcut = CMD + 6)
Ctrl + drag from the object to the label, choose the outlet messageLabel.
Ctrl + drag from the button to the object, choose changeLabel.
Now, to display text when you press the button, you would add code to the changeLabel IBAction to do so.
e.g. [messageLabel setTitleWithMnemonic:#"blah"];
And Finally, if you want it to automatically display text, you might as well just change the label content in Interface Builder / place the code in the windowDidLoad method in your controller.
That to me is pretty much the simplest way for you to do it. However, I recommend you read some books / tutorials on Cocoa and Objective-C before delving into harder stuff.
You'd programmatically make changes to your window in the code for your window controller, of course. Open AXWindowController.m and do whatever it is you're wanting to do. It's hard to give a more definite answer without knowing exactly what you're trying to do (if you just want to create the interface, use Interface Builder) or what your experience level is.

Creating Cocoa PopUpMenus programmatically and Getting code form a GUI item built with the interface builder

I need to create many Cocoa items programmatically and most resources I can find focus on creating GUI with the builder. Is there any way to get the objective C code for an interface item created using the interface builder in Xcode? Also, I am a little confused about PopUpMenus. Are PopUpMenus contained inside PopUpButtons or are the independent? Also, can anyone link me to a good description of how to create PopUpMenus programmatically Using Cocoa and Objective-C?
It depends on what you mean by "PopUpMenus". In Cocoa a menu is a menu, specifically an instance of NSMenu. NSPopupButtons have an associated NSMenu object which you can assign to the button using the -setMenu: method.
You can certainly construct menus programmatically using the NSMenu and NSMenuItem classes. Take a look at the various -addMenuItem: methods in the NSMenu documentation.
You can also access the components of a menu created in Interface Builder programmatically if you have a reference to the menu, specifically an outlet instance variable that is connected to the menu in Interface Builder.
If you're referring to contextual menus that appear when you click the right mouse button, all you need to do is connect your menu to the view/control you want to attach it to with the -setMenu: method. You can make this connection in Interface Builder also.

Putting an NSPopUpButton in an NSToolbar

Having wrestled with NSPopUpButton in a previous question here I am now trying to place a NSPopUpButton inside an NSToolbar. Essentially I want to create something similar to what XCode has by default on the left hand side of it's toolbar. E.g. a pop up button with an action button next to it.
I have seen a method that show's a programmatic way of creating an NSPopUpButton and then adding it to an NSToolbar, but then I can't work out how to do all the Binding stuff that was so handy last time.
Interface Builder hasn't been very helpful, so any help gratefully received.
P.S. Could I solve this by creating a custom view (containing an NSPopUpButton with the usual bindings) and then adding the custom view to the toolbar?
It's actually pretty easy to do what you want here. In Interface Builder, switch to the tree view (the second button on the View Mode segmented control). Expand the window and the toolbar. Then, from the library, drag a popup button onto the toolbar. Interface Builder will embed a new popup button in a custom view for you automatically.
To actually put the button on the toolbar, double click on the toolbar in the window. This will bring up the customization sheet. You can drag the popup button to the desired location on the toolbar.
If you wanted to do this programmatically, you would create a custom view containing your popup button. Then, you'd need to assign it to a new outlet so you can refer to it programmatically. In the toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar method, you would create a new NSToolbarItem per usual, and call setView: to assign the custom view to the toolbar item.
Old post, but note you can also double-click the toolbar in the window and drag straight into the "Allowed Toolbar Items".
You will want to open up the toolbar view anyway so you can drag things in or out of the default toolbar items.