Re: "Tricks" of the "Trade"
Re: "Tricks" of the "Trade"
- Subject: Re: "Tricks" of the "Trade"
- From: Greg Titus <email@hidden>
- Date: Fri, 8 Jun 2001 12:16:27 -0700
On Friday, June 8, 2001, at 09:40 AM, Erik Thorteran wrote:
I know about that, however the problem is, when you don't have a single
nib anymore, then the whole thing starts to break up in my mind,
because the classes no longer know about each other through IB
connections, how do you get around that? Notifications seem like a
rather ungainly approach.
There are 3 main approaches to communicating with objects in other nibs:
notifications, singleton classes, and utilizing File's Owner.
Notifications tend to work best for things like inspector panels. You
can put a Find panel or Info panel in some other nib and then use
notifications to basically say: "object X is selected now". The Info
panel / Find panel / whatever can then keep track of the selected object
and display whatever information is appropriate, or declare some
protocol for interacting with the selection. (For some generic framework
code that does this sort of thing you could look at OmniAppKit at
http://www.omnigroup.com/community/developer/sourcecode/)
Notifications also work well for user defaults. Have your preferences
panel send a notification "default X has changed" or simply "my defaults
have changed", and then any other objects in your application which need
to change their behavior based on those preferences can register for
those notifications.
User defaults is also a good demonstration of a singleton class. (Not
exactly since there actually can be more than one, but usually you only
deal with +standardUserDefaults.) A singleton is a class that you only
ever have one of, and usually there is some class method for getting at
the single instance. NSSpellChecker in AppKit is another example. You
use singletons where you know that there is only going to be one of
something in your entire app, and then you can communicate with the
singleton wherever necessary by getting it from the class method.
Using File's Owner is the third main way of communicating between nibs.
The File's Owner isn't an actual object in the nib, it is a placeholder
whose connections actually get hooked up to whatever object you pass in
as the owner in the -loadNibNamed:owner: method. So whenever you load a
nib, you automatically have a connection to that one object in the nib
(since you had to have it before to pass into the method...)
A common pattern with File's Owner is to have a class with
-loadNibNamed:owner: called from that class's -init method, passing
itself as the owner. (This is how NSWindowController works.) So you can
create a new NSWindowController subclass, instantiate it, and refer to
any of the objects in that controller's nib (that it is hooked up to) by
calling methods on the controller which you just instantiated.
Hope this helps,
-Greg