Re: Connect NSView to NSDocument
Re: Connect NSView to NSDocument
- Subject: Re: Connect NSView to NSDocument
- From: Greg Herlihy <email@hidden>
- Date: Sat, 04 Mar 2006 17:52:20 -0800
- Thread-topic: Connect NSView to NSDocument
I have found that supplying both an instance variable and a matching
accessor routine enables the Nib loader to establish a connection quite a
bit sooner than it otherwise would with just the instance variable by
itself.
In particular, without a accessor routine for the instance variable
("document", in this example) the connection to the File's owner has usually
not been established (at least in my experience) by the time that
-awakeFromNib is called. But with an accessor routine available, the
connection has been established. (And not having the connection established
by the time awakeFromNib is called is rather inconvenient, since the object
must then arrange for itself to be called later on.)
In this case, the set accessor routine for document could be written as:
-(void)setDocument:(id)doc
{
document = doc;
}
Of course, an actual application may prefer a less generic name than
"document" for its instance variable. If so, the name of the routine would
also have to change. The name of the accessor routine that the Nib loader
looks for, follows the simple formula: "set" + capitalized name of the
instance variable to be connected.
Greg
On 3/4/06 3:19 PM, "James Bucanek" <email@hidden> wrote:
> Jason Barker wrote on Saturday, March 4, 2006:
>> I have a Cocoa Document-based Application project with a MyDocument
>> class and a subclass of NSView in my MyDocument.nib. I have some
>> objects in my MyDocument class that I would like my subclass of
>> NSView to be able to access. How can I do this? Do I have to set up a
>> connection between these objects in Interface Builder or through Xcode?
>
> You need to add an outlet to your custom that you then set in Interface
> Builder.
>
> In your subclass of NSView, simply define an outlet that points to your
> document object:
>
> @interface MyView : NSView
> {
> @public
> IBOutlet MyDocument* document;
>
> ...
>
> Import this class into IB then connect the document outlet in your instance of
> MyView to your instance of MyDocument. When the NIB is loaded, the AppKit
> framework will set the MyDocument* pointer in your MyView object to the
> instance of MyDocument. Now, any of the methods in MyView can (indirectly)
> access whatever they need in MyDocument.
>
> [ Note: Don't count on the connection to be set until the NIB has finished
> loading. ]
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden