alecail
alecail

Reputation: 4062

How to refer to Cocoa controls located in in the same NSWindow?

I have a NSWindow with an NSView and an NSTextField inside.

I'm using Interface builder right now. I have dropped the two controls on the default NSWindow and subclassed an NSView. I'm implementing the -drawRect method from NSView and I need to access to the content of NSTextField.

How do I refer to the instance of NSTextField from a method inside the NSView ?

Upvotes: 0

Views: 168

Answers (1)

Roger
Roger

Reputation: 4797

Your NSWindow is (or should be) controlled by a window controller. In IB you create an outlet for the NSTextField in your window controller. Using the outlet, you can then refer to the NSTextField:

In your window controller .h file:

@property (strong) IBOutlet NSTextField *myTextField;

In your window controller .m file:

@synthesize myTextField;

From there you can in your controller:

[[self myTextField] setEditable: NO];

A point to note is that you do not access the controls in a window directly from that window as windows (and all Cocoa controls for that matter) are statically stored in a XIB/NIB file. All access to controls (UI elements) is channelled through controllers (NSWindowController, NSViewController) which in turn are capable of loading XIB/NIB files.

Apple provides various samples in their docs on how to do this.

Upvotes: 2

Related Questions