sebastijan said:
I don't understand "myWindow". I have a window with name "myWindow" however this is not enough.
Code:
/* myController */
#import <Cocoa/Cocoa.h>
@interface myController : NSObject
{
}
- (IBAction)openWindow:(id)sender;
@end
#import "myController.h"
@implementation myController
- (IBAction)openWindow:(id)sender
{
[myWindow orderFront];
}
@end
Where and how I must declare myWindow?
There are 2 ways to do this:
- In code: you can create a new window by using the initWithContentRect:styleMask:backing:defer:screen: method, but that involves doing everything in code and is tedious and "un-sexy"!
- In Interface Builder:
These are steps which the book probably goes through, it may seem complex here, but it's worth learning as you'll probably be doing this very, very often.
1) As an example, try creating a new Cocoa application project.
2) Open the MainMenu.nib in IB (Interface Builder).
3) By default, Xcode will add one window to each new project. Now, all you need to do is add the code to show it.
4) Click on the "Classes" tab in IB.
5) Scroll over to the left, and select "NSObject".
6) Control-click on it, and select "Subclass NSObject".
7) Name the new class you've just created "Controller". (This creates a class we can use for our window-handling code).
8) Control-click on the newly created "Controller", and select "Add Outlet to Controller". (This creates a pointer in the controller class, which we can use to point to our window).
9) Set the outlet name to "myWindow"; and select "NSWindow" from the drop down list on the right hand side. (Now, we've specified this pointer will be of type "NSWindow*)
10) Select "Controller" again, and select "Create Files for Controller" - just click "Ok" on the following Save As type dialog. (This creates the source code files for the Controller class.
11) Control-click on "Controller", and select "Instantiate Controller". (This creates one instance of the Controller class, and will also take you to the Instances tab in IB).
12) Now, hold down Control, and drag from the Controller icon (a blue box) to the Window icon in the Instances tab. When you release, click on the Outlets tab if not already selected. Click on "myWindow", then click the Connect button. (This means the "myWindow" pointer/outlet in Controller is now pointing at the window that was created for us in MainMenu.nib).
13) Save MainMenu.nib, and go back to Xcode.
14) Under "Other Sources", you should now have "Controller.m" and "Controller.h" files. Open Controller.m.
15) Add the following code:
Code:
- (void) awakeFromNib
{
[myWindow makeKeyAndOrderFront] ;
}
16) Save, build, run.
If you've any problems, ask here.