Re: How to get my class instance that's in IB
Re: How to get my class instance that's in IB
- Subject: Re: How to get my class instance that's in IB
- From: Graham <email@hidden>
- Date: Thu, 21 Feb 2008 14:22:18 +1100
On 21/02/2008, at 1:23 PM, Russ McBride wrote:
But if there's an instance of my class then I should be able to
call a method on that instance without having to make a new
instance. But how do I get ahold of that IB-created instance of my
class so that I can call one of its methods??
You need an outlet connected to the instance.
The outlet needs to belong to an object you already know about - in
other words already instantiated in your program.
The NIB file contains an icon labelled "file's owner". This is a
proxy for some existing object. In the case of MainMenu.nib, it
stands for the main application instance (NSApp). So if you could an
an outlet to NSApplication, you could link it to your custom object
instance in IB. However, this is a bad idea in this case (it's
simpler for document windows where File's Owner is usually a subclass
of some document class you have derived from NSDocument).
Instead, for MainMenu.nib it's better to create another object that
is the application delegate, and put your outlet there. You can do
all of this in IB then get it to create the files for you, or you can
create the class in Xcode and drag the header file to IB. The
application delegate can simply subclass NSObject.
Use control-drag in IB to connect outlets to instances. (Drag FROM
object with outlet TO instance, then select the particular outlet to
use). Same to connect your application delegate to the <delegate>
outlet of NSApp (file's owner).
File's Owner basically is your "way in" to a NIB - it stands for an
object within your app already. If you create connections from there
to any object in the NIB, you can get to those objects. If you load a
NIB directly, you specify the object that is File's Owner. For
MainMenu.nib it's the app, and for document NIBs it's the NSDocument
instance.
--------
S.O.S.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden