Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sebastijan

macrumors member
Original poster
May 22, 2005
62
2
London, UK
Ok. First I have a book Programming in Objective-C - it is really cool. But how can I open a new window or close a window in Cocoa? I now how to make controller, buttons... but I don't now how to write this (for open or close window)?

P.S.: Sorry for my bad English.
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
To show a window "myWindow":

[myWindow orderFront] ;

(or [myWindow makeKeyAndOrderFront]; whichever suits your needs)

To hide a window:

[myWindow orderOut] ;

(or [myWindow close]; or [myWindow performClose]; )

See the Xcode documentation to see which one you need to use; the best way is to search the documentation for NSWindow, then select "Method Types" from the drop down list.
 

sebastijan

macrumors member
Original poster
May 22, 2005
62
2
London, UK
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?
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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.
 

sebastijan

macrumors member
Original poster
May 22, 2005
62
2
London, UK
thanks, that you took time for explanation.

- what is better: to have multi .nib files or all window in one .nib?

- how can i run only one window in one .nib if I have 5 window? (I must hide other window on run?)

- how can I make 3 window in one .nib and run only one window that have two buttons ... "Open Window 2" and "Open Window 3" and window 2 and 3 have a buttons hide and close.

picture2798912.png
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
Whether to use one nib or many is up to you and the design of your program, but if you're just learning, keep all the windows in one nib for simplicity for now.

If you want to add more windows after creating Controller.h and Controller.m; here's one way to do it:

1) Open Controller.h in Xcode.

2) You should by now have a line: IBOutlet NSWindow *myWindow ;

3) If you need to add two more windows, just add two similar lines underneath:

Code:
IBOutlet NSWindow* myWindow2 ;
IBOutlet NSWindow* myWindow3 ;

4) Save the file.

5) Now, here's a useful trick: Drag the Controller.h file from the Xcode window into the MainMenu.nib window in IB. (This tells IB "Look! The Controller class has changed, and now has 3 NSWindow outlets").

6) If you go to the Instances tab, and double click on the Controller object (blue box), you should see the three outlets listed.

7) Create two new windows in IB (Hit Command-/ to show Palettes, select the Windows palette, then drag a window out of the palette and release - do this twice for two windows).

8) Connect the outlets to the windows as before; by control dragging from Controller to the new windows, release, then select the outlet name and clicking connect.

9) Save in IB, then build in Xcode.

10) Now, you have 3 window outlets that you can show or hide at will.
 

sebastijan

macrumors member
Original poster
May 22, 2005
62
2
London, UK
thanks.

Why warning for orderOut?

warning: 'NSWindow' may not respond to '-orderOut'
(Messages without a matching method signature will be assumed to return 'id' and accept '...' as argument.
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
sebastijan said:
thanks.

Why warning for orderOut?

warning: 'NSWindow' may not respond to '-orderOut'
(Messages without a matching method signature will be assumed to return 'id' and accept '...' as argument.

Ah, that's my mistake. It's not

[myWindow orderOut];
but:
[myWindow orderOut:self].
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.