Adding a new window in Cocoa - objective-c

I'm a beginner with Cocoa programming. All I wnated to know is how do I create a new window in Cocoa. I have created a NSViewController and window. As shown in tutorials I was able to add buttons to the MainMenu window and add actions to the buttons. Now I added a new NSViewController. I then went to plist file and made to load it first. My questions are as follows.
When adding a new NSViewController, h m and xib files were created but the XIB file just has custom window and no other control elements like minimize max buttons etc.
Secondly I added few elements and ran the file. The application ran but nothing was displayed on screen. Am I missing something.

You mostly only create a new NSWindowController if you add a new Nib-File.
The advantage of separating your windows or separate views in Nib-Files can be:
Making your code more organized
Easy instantiation of multiple windows of the same type (like browser windows)
If you don't need this, you can simply add a new window to your MainMenu.xib.
Then you can either let the window be visible at launch, or make an outlet to it and make it visible whenever you desire.
Otherwise you can go to your files -> Add new file -> Subclass of NSWindowController
There will be a checkbox to automatically create the xib-file for you, make sure to check.
Now just make sure to initialise with initWithWindowNibName:, and call showWindow: on it.
MyWindowControllerSubclass *wcs = [[MyWindowControllerSubclass alloc] initWithWindowNibName:#"TheNameOfMyNib"];
[wcs showWindow:self];

I was using XCode 4.2. Now I updated to 4.6.3. Now while creating NSWindowController xib file creating option is there. SO its done directly.
Otherwise you need to add a window controller and a new Window XIB. Later you can add an Object to properties tab. Then change the class to the Window Controller and link the Window to the Object.

If you want to add the view controller to a window then, it is fairly simple. NSViewController has a property named view. You would add this view to the window.contentView.
You would typically create a new xib file with a view and setting the file owner to your view controller. Then in the -(id)initWithNibName: method you could call the super with the nib name of your view to instantiate the controller with its view.
If you want to show a new window. Then create a new NSWindowController object and instantiate it, with the nib of your window and calling window on the controller should bring the window to front. If your window has already been initialised then, you could call methods like orderFront:, orderBack: on the window of the controller.

Related

Setting NSWindowController for the root window?

I am playing around with the default XCode template for a cocoa application. By default it has a MainMenu.xib with a window and a menu -- awesome.
I intend to build up my views/subviews in code, so I then create a RootView : NSView and a RootViewController : NSViewController, and bind them up in IB so that my base view in that window is my RootView, controlled by my RootViewController.
Enter my question -- How do I do the same to have a RootWindowController bound to the NSWindow? I had assumed File Owner was the trick, but it's set to NSApplication, which I think is correct. The main goal of NSWindowController in my case is to manage the toolbar based on NSNotifications fired from other services -- is this even the correct design here? I don't think I can do this from my RootViewController since I don't have a window reference, but is that where I should do it instead?
Many times, the application delegate is used as the main/central controller for a non-document-based application. This is a good place. This can be an NSWindowController instance if you like. You can just do away with the existing window in your nib.
Helpful information from the NSWindowController API reference:
You should create a subclass of NSWindowController when you want to augment the default behavior, such as to give the window a custom title or to perform some setup tasks before the window is loaded. In your class’s initialization method, be sure to invoke on super either one of the initWithWindowNibName:... initializers or the initWithWindow: initializer. Which one depends on whether the window object originates in a nib file or is programmatically created.
But if it were me, I wouldn't bother going the NSWindowController route; I'd just add this logic to my app controller (and set it as the app delegate) since this is app-level stuff. No need to add a layer of complexity unless you've got a LOT of main-UI-updating complexity you want to compartmentalize.

XCode 4 - create AppController standard?

I'm using XCode 4, and note that when setting up a new Cocoa Application project, you get an AppDelegate.m and .h file, as well as a .nib (.xib). Using alt-command-return, you get the 3-column editor layout, from which you can control-drag from controls to the AppDelegate.h file to create Outlets or Actions.
My question is, is it recommended to utilise these AppDelegate files to manage your interface controls ie. updating labels, acting on button presses etc. OR is it better to create an AppController class, add an object to the .xib and subclass it to AppController, modifying AppController to mange the controls? If this is the case, what are appropriate uses of AppDelegate?
I would like to say, it is just a matter of development style. What I do is use another class and change the AppDelegate to that class/view and use. Also I do not draw outlets and button on the default window. I make a view and add those as subviews to the mainWindow.

How to override NSWindow?

I need to create a custom window, so I have created a subclass of NSWindow. I overode the constructor and a bunch of other functions.
I need to replace the current window with the window subclass I created. I know it has something to do with Interface Builder but have no idea what to do. How would I do this?
I tried removing the NSWindow from the Inspector, and instead adding my custom window, however this only results in no window showing up during runtime.
I also edited the app's delegate to change NSWindow to my custom window and also changed the delegate's "main" outlet to my custom window.
I am on Mac OSX Snowleopard using Xcode 3.2.6
First, you need to make sure Interface Builder knows about your custom class. To do this, open your xib file and go to "File->Read Class Files…", and choose your header file.
Then, select the window you want to change and open the inspector. In the last tab of the inspector, under "Class Identity", there is a text field labeled "Class" simply type your class's name here.
After you do this, your custom class will be used as the class for that window. It will look like a normal window in IB, but will let you use custom outlets and actions, and will use your class when you actually run your project.
go to the third tab of the utilities section when your window is selected
enter your custom class name under 'Custom Class'

How can I make sure a window is only displayed once in Cocoa?

I have an NSWindow which contains an NSImageView. This window gets activated everytime I click on a cell in my tableview. I only want 1 instance of the NSWindow to appear, but want to be able to change the contents of NSImageView.
How can I initialize NSWindow and display only 1 instance of it?
This is a job for a singleton!
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32
One possibility to do this is to create an NSWindowController subclass and an associated window XIB that gets loaded when the window controller is instantiated.
I'm sure you already have some controller class handling the mouse click in the NSTableView. In that class, simply keep around an instance of the NSWindowController subclass mentioned above as an instance variable. Whenever you need to display the window, tell that ivar to display its window.
If the window's contents are dependent on the clicked table cell, simply add some methods to the window controller that modify its window's contents and call these methods in your click-handling method before you display the window.
btw: I wouldn't use a singleton here because in this case it would just be a workaround for bad design (just my opinion, not a hard fact).

Adding a custom view to a window in Cocoa

I am creating a sample application for Mac OSX. I created a window which contains a login button and I added a new custom view. Now I need to load the custom view to the window when the user clicks the login button. Please anyone help me...
Will this help?:
[[window contentView] addSubview:customView];
[window setContentView:customView] will also do the job.
You also need to add IBOutlet variables for your window and custom view to the header file for your controller, and drag that onto the Interface Builder document window. Then control drag from the controller to the window and view, and select the variables.
(and you need to change the controller to be an instance of your custom controller class, which you do by selecting it in Interface Builder and going to the Inspector)