2

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.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
David Bleu
  • 21
  • 3

3 Answers3

1

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
  • 1,491
  • 11
  • 11
  • 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 Apr 01 '10 at 13:47
  • 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 Apr 01 '10 at 16:52
0

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

Community
  • 1
  • 1
Vincent Zgueb
  • 1,491
  • 11
  • 11
0

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
  • 18,848
  • 5
  • 41
  • 49
  • 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 Apr 02 '10 at 22:47
  • 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 Apr 02 '10 at 22:50