PDA

View Full Version : instance variable 'viewController' accessed in class method




Sergio10
May 5, 2009, 04:39 PM
Hi,
I need to return UIViewController pointer for using in other classes. So I write:

+ (UIViewController*)viewController
{
return viewController;
}


@interface MyApp: NSObject <UIApplicationDelegate> {

UIWindow *window;
UIViewController *viewController;
}
+ (UIViewController*)viewController;

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UIViewController *viewController;

@end

And I have next warning:

instance variable 'viewController' accessed in class method

How to solve this problem?



BlackWolf
May 5, 2009, 07:19 PM
you should get over some of the basics again!

you define the method with a '+' - that makes it a class (static) method. and you cannot access any instance variables in such methods by definition (since it is NOT an instance method you cannot access instance variables because they simply do not exist).

you need to replace the + with a - to make it an instance method.

but btw: you don't have to write your own accessor methods - simply synthesize your property with the @synthesize keyword and this will be done for you if you only need the standard accessor methods!

Sergio10
May 6, 2009, 02:40 AM
This variable is allready synthesized. how to call this variable from another class?

BlackWolf
May 6, 2009, 07:19 AM
This variable is allready synthesized. how to call this variable from another class?

from another class? you have to pass it there somehow. for example as a parameter of a method.
if the variable is already synthesized why are you writing a getter method for it?

Sergio10
May 6, 2009, 08:09 AM
BlackWolf, So how to get value from another class?? Is it possible?
like this: MyApp.viewController.view = nil;

BlackWolf
May 6, 2009, 08:49 AM
BlackWolf, So how to get value from another class?? Is it possible?
like this: MyApp.viewController.view = nil;

you need some kind of reference to something. like I said: pass your viewController as a method-parameter and then you can store it wherever you want.

in your example: What is MyApp? As long as your class doesn't know what MyApp is, it can't use it of course. If you pass your AppDelegate as a method parameter and then store it in the instance variable MyApp your class will know what MyApp is and therefor it can access it, otherwise it (obviously) can't