Using NSWindowController to manipulate a window - objective-c

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.

Related

Changing focus from NSTextField to NSOpenGLView?

I am developing an interface for an OpenGL Simulation on the Mac using Interface Builder. All OpenGL stuff is being written in C++ (which I know fairly well). Unfortunately I have no idea about Objective-C.
Basically, I have a few NSTextField displaying info on an object selected on the Screen. Using these textfields the user is the able to manipulate the object on screen and then there is a Save Button and a Restore Button (which either save the new values or restore the original ones)
I have this all working. My problem is when I enter data into an NSTextField the "focus" of the windows seems to remain on the NSTextField (blue border remains around it).
I use the keyboard to interact with items within the NSOpenGLView, I need to pass the focus back to the NSOpenGLView when I hit either the Save or Restore buttons.
Sorry if this is a very straightforward question
Thanks
David
Have you tried using NSWindow makeFirstResponder method to make your NSOpenGLView the first responder?
Just managed to get it working.
I had to add the line:
[[NSApp keyWindow] makeFirstResponder: MyOpenGLView];
to the end of the function being called when I click on either of my buttons.
(Thanks Julio for pointing me in the right direction)

Drawing an "NSView" to a Custom-View - How? Am I taking the right approach?

I'm using Objective-C and Cocoa, whilst developing for Mac OS X - so not the iPhone/Cocoa Touch. (That said, I'd be interested if it was the same procedure for the iPhone)
I'm working on a preferences window for a simple app. I have a NSWindow with a toolbar - there are 5 different items on the toolbar, all of which need to bring up a different set of options.
So I set the NSToolbar and its items in Interface Builder, and then placed a custom view underneath the menu - taking up the rest of the window. My plan is to work out the interface too each of the NSToolbarItems options, and then draw the corresponding view on to the custom view when the specified NSToolbarItem is clicked.
I'm guessing that I simply create a NSView sub-class for each view, an empty xib in Interface Builder - set the xib to my custom NSView, code it as usual... But here's a few problems;
1 - Just how can I get the xib file to appear on the custom-view then? I have looked around and most articles don't seem to have this situation, or a situation I can relate too.
2 - When the window comes up, I want the default view to appear on the custom view. Once again, I'm guessing I just write that in the initialisation code for the NSWindow - its no big deal. It just goes back to question 1 though - how do I draw my NSView to the custom-view specified in Interface Builder?
I'd be really grateful for any help!
Cheers in advance.
So I set the NSToolbar and its items in Interface Builder, and then placed a custom view underneath the menu - taking up the rest of the window.
You can't have a menu inside of a window. You can have a pop-up button, which has a menu, but not a menu directly. Did you mean “toolbar” here?
You don't need to create a custom view for this. Make a tab view and set it to be tabless. Give it as many tab view items as you have toolbar items. In your controller, write an action method for each of the toolbar items, and in each action method, switch the active tab of the tab view.
You can activate different tabs in IB to populate them with views in IB. The active tab is saved in the nib, so make sure you set it back to the first tab before saving, so that the first tab is the one that's initially active when your app runs.
Just how can I get the xib file to appear on the custom-view then?
That question doesn't make sense.
Once again, I'm guessing I just write that in the initialisation code for the NSWindow - its no big deal.
You would only be able to do that if you have your own initialization code for the window, which you would only have if you have subclassed NSWindow. There are very few reasons to do that; unless you're making the window itself look different (not making an Aqua or HUD window), you should move that initialization code elsewhere, probably to the aforementioned controller (which should be the File's Owner of the nib).
It just goes back to question 1 though - how do I draw my NSView to the custom-view specified in Interface Builder?
A custom view in Interface Builder is a plain NSView (unless you explicitly change it to a subclass of NSView you create). However, you do not need one for anything you have described in your question.

How to get Interface Builder name from id

If I name a widget in Interface Builder, and then I write a method that receives click events on that button:
- (IBAction)btnTouchDown:(id)sender
{
// how can you identify the button here,
// if several different buttons map
// their "Touch" event to this same function?
// I know you can look at its text but that seems really clumsy
// can I somehow get its INTERFACE BUILDER NAME?
// I named each uniquely in interface builder,
// under "Identity"/"Name"
// Or is my only recourse to tie EACH BUTTON to its own handler function?
}
Click on the button in interface builder, press CMD+2 (Or in info dialog, second tab) and drag the circle from Touch up inside to File's Owner from the yourView.xib window and then it'll let you select one of the methods which has the - (IBAction) method:(id)sender signature. Choose the one you want to associate with that button :)
You say you don't want to make a method for each button, but actually that is the best way in my opinion, rather than having one huge monolithic method which handles each of the buttons and checks each time to see which one of them it is. Using the process I mentioned above, you can specifically tie a handler method to a particular button, keeping things nice, tidy, and logical.
If you really want to do that though, you can also test for the tag number.
you have to define the button as an instance variable in your view controller and then use the IBOutlet pragma to make it known to IB. So for example you would do this in your view controller:
UIButton IBOutlet *mybutton;
After defining it like this it will show up in your IB connections where you can associate it with the appropriate button

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.

NSTableView responding to first click in a panel

I have noticed in the Interface Builder if I want to click on or drag from the Library panel, I only have to click on it once, even if the Library panel does not have the current focus.
I am trying to build a panel that behaves similarly.
Is there any simple way to let the NSTableView accept the click, even if the window does not have the focus?
Thanks.
Ok, I found the answer. Inside from awakeFromNib I call this:
[self setBecomesKeyOnlyIfNeeded:YES];
It seems to do the trick. It's a little bit different from Interface Builder where the Panel actually gets the focus simultaneously with a single click, but doing it this way is just what I was looking for.
Your view should override -acceptsFirstMouse: to return YES (or evaluate the event passed to you to determine what to return). You'll have to subclass NSTableView to do that of course.