Re: [NSApp setDelegate]
Re: [NSApp setDelegate]
- Subject: Re: [NSApp setDelegate]
- From: Mark Ericksen <email@hidden>
- Date: Tue, 5 Jun 2001 13:18:26 -0700
Robert,
When building Cocoa apps, many object "connections" are handled within
Interface Builder. In your case and in the sample apps Sketch and
TextEdit, the application's main nib file contains a special object
known as the File's Owner which has automatically been set to
NSApplication. This is the real application object that will be used
when your application is running. A common design pattern used in Cocoa
applications is to have your Application Delegate be your primary
controller object. To use this design pattern, do the following:
1. Open your MainMenu.nib in Interface Builder.
2. In the Instance tab, you'll see an icon that looks like the default
app icon, titled File's Owner. This object is your NSApplication object.
3. Click on the Classes tab.
4. Create a subclass of NSObject and call it AppDelegate or Controller
or what ever you want.
5. In the Classes menu (or with the contextual menu) choose Instantiate.
6. You will be taken back to the Instances tab and a new icon will be
there with the title as the name of the class.
7. Click and hold down the control key over the File's Owner icon.
8. Drag a connection to your new controller object and let the mouse
go. In the Connection Info window (use Tools->Show Info in case it
isn't visible and select Connections) you should see a short list of
connection options, in your case there is only one options labeled,
delegate. This list shows the instance variables the class,
NSApplication, is willing to let developers connect to in Interface
Builder.
9. Double click on the word, delegate, and you should see a dimple
appear next to the delegate title.
You have now made a "connection" between your controller object and the
NSApplication instance in your application. When your program runs, and
the MainMenu.nib is loaded (this should happen automatically) all the
objects in the nib are unarchived (not initialized) and all the
connections in the nib will be connected, including your delegate
connection.
Of couse now you need to create the code files and implement what it is
you want your controller object to do as the application delegate, but
I'll leave that up to you! :)
Cheers,
Mark Ericksen
Jiiva, Inc.
On Tuesday, June 5, 2001, at 10:55 AM, email@hidden wrote:
Please forgive me if this question is exceedingly stupid.
I'm trying very hard to work my head around creating a delegate for my
application, but am having some trouble. I know what delegates do, and
I know why I need them. What I don't know is how to instantiate them.
NSApplication has a method [NSApp setDelegate] which (I would assume)
would be called either in the NSApplication's init: method, or from
another already instantiated class. Looking through Apple's supplied
examples (Sketch and TextEdit), I don't see this method being called,
even though each app has a delegate. While looking at the source for
the apps, I notice that neither delegate is called from code or has a
custom init: method. With out calling [NSApp setDelegate] how does an
app know which class should implement the delegate method? Does NSApp
send a [myClass respondsTo:] method to all instantiated classes? Is
there a setting in IB or PB that I am missing?
--Robert Lee Dotson
"The illegal we do immediately. The unconstitutional takes a little
bit longer."
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev