macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Working with Sheets in Cocoa
Pages: 1, 2, 3

You can see one of the practical differences between modal sessions and modal loops simply in the execution flow in openPrefsSheet:. When we invoke beginSheet... a modal session is started, but the application goes on with executing openPrefsSheet:. However, at the point where we invoke runModalForWindow:, the application's event loop pauses while the indicated window runs in a modal loop. That is, not only is prefsWindow the only window that can accept events, but execution of our method stops until the modal loop ends. The application will remain in this state until some action in prefsWindow sends one of three messages to NSApp: stopModal, abortModal, stopModalWithCode:. This will be taken care of in the action method connected to our Close button-closePrefsSheet. Let's take a look at that method now.



The method closePrefsSheet does one simple thing for us -- indicate that the sheet should be closed by stopping the modal event loop for prefsWindow. All we do in closeMethod is the following:

- (IBAction)closePrefsSheet:(id)sender
{
    [NSApp stopModal];
}

And with that our application resumes execution of the method openSheet at the point directly after where we started the modal event loop. Now -- back in openPrefsSheet: where we left off -- we simply send a couple of messages that ends the sheet's modal session, and removes it from the screen. endSheet: ends the document modal session began by beginSheet..., and orderOut: removes it from the window. The argument of orderOut: is the message sender, which in this case is self.

Try rebuilding now and see the results. You can download the completed project folder here. Your preferences sheet should look like the image below.

Screenshot.
The preferences sheet.

Because we only have one button in our window (other than the column-selector switches), the close button, stopModal, is sufficient for our purposes. If we had more than one button, however, than we could do several things to handle the different actions of different buttons. The obvious solution is to put the button-specific code in each button's action method. In that way, the button-specific code would be executed when its button is pushed, and each of these action methods would have a stopModal message to NSApp like we have above.

Learning CocoaLearning Cocoa
By Apple Computer, Inc.
Table of Contents
Index
Sample Chapter
Full Description
Read Online -- Safari

The other option is to have each button's action send a stopModalWithCode: message to NSApp, where the argument is an integer. What happens then, is that the integer passed as the argument to stopModalWithCode is returned by runModalForWindow:, and we can store that in a variable for later use. In this way, each button's action methods can take on a unique int identifier, allowing us to determine which button was pressed, and then executing code based on that.

In simple situations, it is probably better to just run whatever code you need within each button's action method, but I wanted to point out and make you aware of the available options.

As a fun aside, I want to tell you how to change the transparency of a window. You might have noticed that the transparency of our preferences window was changed as a result of it being a sheet. We could change it if we wanted to using one simple line of code, that we could put in awakeFromNib:

[prefsWindow setAlphaValue:0.50];

This would make a pretty transparent window, which is probably not so useful for a window with a lot of controls like we have. You can see the results of this in the image below:

Screen shot.
A very pale preferences sheet.

That's it for now! That's how we create sheets. Not too difficult, I think, so have fun with it. This column is the last time we'll talk about AddressBook for a while. Next time, we'll begin our foray into Cocoa graphics, which will last for some time. See you next then!

Michael Beam is a software engineer in the energy industry specializing in seismic application development on Linux with C++ and Qt. He lives in Houston, Texas with his wife and son.


Read more Programming With Cocoa columns.

Return to the Mac DevCenter.