instance variable 'viewController' accessed in class method

Discussion in 'iPhone/iPad Programming' started by Sergio10, May 5, 2009.

  1. macrumors regular

    Joined:
    Oct 3, 2007
    #1
    Hi,
    I need to return UIViewController pointer for using in other classes. So I write:
    PHP:
    + (UIViewController*)viewController
    {
        return 
    viewController;
    }
    PHP:
    @interface MyAppNSObject <UIApplicationDelegate> {
        
        
    UIWindow *window;
        
    UIViewController *viewController;
    }
    + (
    UIViewController*)viewController;

    @
    property (nonatomicretainIBOutlet UIWindow *window;
    @
    property (nonatomicretainIBOutlet UIViewController *viewController;

    @
    end
    And I have next warning:
    How to solve this problem?
     
  2. macrumors regular

    Joined:
    Apr 9, 2009
    #2
    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!
     
  3. thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #3
    This variable is allready synthesized. how to call this variable from another class?
     
  4. macrumors regular

    Joined:
    Apr 9, 2009
    #4
    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?
     
  5. thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #5
    BlackWolf, So how to get value from another class?? Is it possible?
    like this: MyApp.viewController.view = nil;
     
  6. macrumors regular

    Joined:
    Apr 9, 2009
    #6
    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
     

Share This Page