Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sergio10

macrumors regular
Original poster
Oct 3, 2007
137
0
Hi,
I need to return UIViewController pointer for using in other classes. So I write:
PHP:
+ (UIViewController*)viewController
{
	return viewController;
}
PHP:
@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

macrumors regular
Apr 9, 2009
244
0
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

macrumors regular
Original poster
Oct 3, 2007
137
0
This variable is allready synthesized. how to call this variable from another class?
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
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

macrumors regular
Original poster
Oct 3, 2007
137
0
BlackWolf, So how to get value from another class?? Is it possible?
like this: MyApp.viewController.view = nil;
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.