views:

195

answers:

5

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.

+1  A: 

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 Zgueb
Thanks for replying. To be more precise; I have an NSWindowController with IBOutlets and I need to access the state/title/... of them from within another implementation file's class method. It's a class method because it is being called from yet another file. I could just keep it with an instance method in the original file but I thought, if I can access the outlets why shouldn't I move the whole method into its own class anyway?
David Bleu
It's not clear for me...Are you ok with the concept of class and instance methods and properties and accessors ? I guess you're trying to access some data encapsulated in one object from within another one but the words you are using are not clear ('called from another file'). The file has nothing to do with the variables scope. Do you see what i mean David ?
Vincent Zgueb
A: 

@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.

@Vincent Zgueb I hope you receive the following message because I can't seem to find the good way to reply to your comments. Anyway, thank you for the example but this still gives me a problem. The object with the ivars is an NSWindowController and I subclassed the File's Owner in IB with it. If I instantiate the object programatically then I won't access the ivars from the original object because I created a new one...
A: 

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

Vincent Zgueb
A: 

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
I see you have the following... Your solution looks good but I keep on getting the warning 'MyWindowController' may not respond to ' -myView'... that's because you have to import appcontroller's .h file in the class where you're accessing that method i.e. #import "AppController.h"
regulus6633
By the way, you'll also have to import the window controller's .h file too so that it knows about the "MyWindowController" class.
regulus6633
A: 

@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...