The title says it all, how do I access properties (title, state,...) of instance variables from within a class method of an other implementation file? I tried @synthesize but I couldn't get it to work. To be more precise; I need to access IBOutlets of an NSWindowController class.
Hi David, First of all, you should read this chapter before.
Introduction to The Objective-C Programming Language.
What do you want to know exactly. Obviously, you cannot access an instance variable without instance. A class method is a static method (message) you can access without any object instance. Could you precise your question David ?
@Vincent Yes, I'm trying to access the encapsulated data. I don't see any other way of accessing data of that NSWindowController object. I need to access the IBOutlets from within another object. ~Sorry for my words like "another file" in the previous comment.
Ok then you just have to declare your properties in your class interface. Your instance variables are prefixed by IBOutlet to indicate that they have to be set using the nib. Maybe you already know all that stuff. Sorry in that case.
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MyClass.h file
*/
@interface MyClass
{
// instance vars
IBOutlet NSString *title; // Do you have this ? Should be bind in IB.
}
// and this to declare the accessors as public methods
@property (nonatomic, retain) NSString *title;
/*
other methods signature declaration
*/
@end
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MyClass.m file
*/
@implementation MyClass
@synthesize title; // allow to generate the accessors of your property
/*
methods implementation here
*/
@end
If you instantiate your class simply call the accessor [myObjectOfMyClass title]. Maybe see the singleton design pattern which is one of the most used and useful to retrieve easily an object instance that have to be unique. What does your Objective-C singleton look like?
Vincent Zgueb
I usually use my appcontroller as an intermediary for things I need accessible throughout all of my classes… assuming your appcontroller is also your application's delegate. From any class I can get to my appcontroller (app delegate) using [NSApp delegate].
With this in mind, I make sure my appcontroller instantiates things like window controllers. Then if I need to access the window controller I create an instance variable for it in my appcontroller and then create an accessor method for that instance variable. For example:
in appcontroller.h:
MyWindowController *windowController;
@property (readonly) MyWindowController *windowController;
in appcontroller.m:
@synthesize windowController;
Then from any class I can get to that instance of the window controller using:
MyWindowController *windowController = [[NSApp delegate] windowController];
@regulus6633 Sorry for creating a new answer again. (I can't find out how to comment). Your solution looks good but I keep on getting the warning 'MyWindowController' may not respond to ' -myView'
when I call NSView *view = [windowController myView];
. I Hope I'm not asking too much here...