Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Working with Interface Windows in Cocoa

Perhaps the most important element of any GUI-based application is the window. In fact, it's so important that those guys from Redmond used it to name their operating system. The windows in an application are like pieces of paper that you use to collect ideas, display information, and record input.

Windows in Cocoa are equally important. Mac OS X continues using the fine window traditions that made the Mac OS famous. All the while, Cocoa builds on and improves those ideas from the past with exciting new features.

Because windows form the basis for nearly all interfaces, you'll use them often. Some of the most common functions that you'll perform include the following:

  • Opening and closing windows
  • Moving windows
  • Adding windows to the Windows menu

In this article, you discover how to accomplish all these tasks and more.

Opening and closing a window

When you work with windows in your Cocoa projects, you'll typically use Interface Builder to design them. Furthermore, if you leave a window open in Interface Builder when you're designing your interface, that window will is displayed when your application runs. Therefore, it's often unnecessary to issue a command to open a window.

Closing a window is a simple matter:

[theWindow close];

When you close a window in this fashion, the window is gone. Its contents are gone from memory. It is no more. That means to see the window again, you have to open a new window object.

Hiding and showing a window

If you'd prefer to keep a window around instead of deleting it from memory, you should hide it instead of closing it. Hiding a window is also an easy task to perform. For example, suppose you have an outlet (named theWindow) as part of your controller class. The outlet represents a window in your interface.

To hide that window, a hideWindow method might look like this:

- (IBAction)hideWindow:(id)sender
{
[theWindow orderOut:sender];
}

The orderOut method of the NSWindow class hides a window from view. It still exists in memory; it just isn't visible. To find out whether a window is visible, you can check the return value of the isVisible method:

- (IBAction)hideWindow:(id)sender
{
if ([theWindow isVisible])
[theWindow orderOut:sender];
}

To make the window appear again, simply use the orderFront method:

- (IBAction)showWindow:(id)sender
{
[theWindow orderFront:sender];
}

If you'd like a window to appear on the screen and act as the main window, intercepting keystrokes, use the makeKeyAndOrderFront method. Cocoa programmers frequently use this method in the awakeFromNib method to force the main window to the foreground:

- (void)awakeFromNib {
[theWindow makeKeyAndOrderFront:nil];
}

Positioning windows

Another important task that you'll need to perform is positioning windows. Before you go bossing windows around, you first need to find out their current position. Use the frame method of NSWindow to discover the origin and size of a window. The method returns an NSRect structure, which contains NSPoint and NSSize describing the window's origin and size, respectively.

After you know a windows origin and size, it's a trivial matter to reposition it. First, set the origin to your liking and call the setFrameOrigin method to apply the new position. This example moves a window ten pixels left of its current position:

- (IBAction)moveLeft:(id)sender
{
NSRect theFrame = [theWindow frame];
NSPoint theOrigin = theFrame.origin;
theOrigin.x = theOrigin.x - 10;
[theWindow setFrameOrigin:theOrigin];
}

To position a window in the middle of the screen, use the handy center method. This example centers a window on the screen:

- (IBAction)centerWindow:(id)sender
{
[theWindow center];
}

Keeping track of windows

Most Cocoa applications have a Window menu to help users keep track of open windows. Normally, you should use this menu as a list of open document windows. (You probably wouldn't display, say, a toolbar window in the Window menu.) The great thing about the Window menu is that Cocoa takes care of it for you. If your window has the following properties, Cocoa automatically adds it to the Window menu:

  • Has a title bar
  • Is resizable
  • Can become the main window

You can exclude a window from the Window menu by altering any one of these properties. If, on the other hand, you have a window that obeys each of these rules, but you still want it to be left out of the Window menu, use the setExcludedFromWindowsMenu method. The awakeFromNib method is a good place to use this call because the method takes effect when your application launches.

- (void)awakeFromNib {
[theWindow setExcludedFromWindowsMenu:nil];
}

Putting windows to work for you

To see how these window features work, do the following:

1. Launch Project Builder and create a new Cocoa application project.

Double-click the Project Builder icon in the Finder to launch it. After it launches, choose the File --> New Project menu. In the window that appears, select Cocoa Application and click OK.

2. Create a new window in the MainMenu.nib file.

Double-click the MainMenu.nib file in the Resources Group to open the file in Interface Builder. In the MainMenu.nib file window, click the Instances tab. Then, drag a new window from the Cocoa-Windows pane of the Palettes window to the MainMenu.nib file window. To follow along with the example, name this new window MenuWindow. You will use it to control the default window that's already part of the interface.

3. Open the new MenuWindow and add three buttons to it.

4. Create a controller class.

To do so, click the Classes tab of the MainMenu.nib file window. Scroll to the left and select NSObject. Press Return to create a new class and name it MyWindowController. Choose Classes --> Instantiate MyWindowController to create an instance of the new class.

5. Double-click the new MyWindowController instance and add outlets and actions to the instance in the Info window.

Add the following outlets: theMenuWindow, theShowButton, and theWindow. Add these actions: centerWindow, moveLeft, and showWindow.

Connect the three new actions to the buttons in MenuWindow by control-dragging from each button t the MyWindowController instance. Click Connect in the Info window to connect each action.

Then connect the two window-related outlets to their corresponding windows. Control-drag from the MyWindowController instance to each window in the MainMenu.nib file window. Click Connect in the Info window to connect to the appropriate outlet.

Finally, connect the theShowButton outlet to the top button in the MenuWindow.

6. Double-click the MyWindowController instance. Choose Classes --> Create Files for MyWindowController and add the header and implementation files for the instance.

7. Return to Project Builder and add this code to the MyWindowController.m file:

#import "MyWindowController.h"
@implementation MyWindowController
- (IBAction)centerWindow:(id)sender
{
[theWindow center];
}
- (IBAction)moveLeft:(id)sender
{
NSRect theFrame = [theWindow frame];
NSPoint theOrigin = theFrame.origin;
theOrigin.x = theOrigin.x - 10;
[theWindow setFrameOrigin:theOrigin];
}
- (IBAction)showWindow:(id)sender
{
if ([theWindow isVisible])
{
[theWindow orderOut:sender];
[theShowButton setTitle:@"Show Window"];
}
else
{
[theWindow orderFront:sender];
[theShowButton setTitle:@"Hide Window"];
}
}
- (void)awakeFromNib {
[theWindow makeKeyAndOrderFront:nil];
[theMenuWindow makeKeyAndOrderFront:nil];
}
@end

8. Test the project.

To do so, choose Build --> Build and Run.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.