Newbie Objective-C Question

Discussion in 'Mac Programming' started by Blakeasd, Feb 11, 2011.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello.
    I have an extremely newbie question. I want to use the window that is declared in my Application Delegate .h file in different .h and .m files. In past projects I just declare the window again. My project is a little larger this time and I want to use the window that has already been declared for me in the App Delegate. In one of my other .h and .m files I did the following:
    Code:
    
    #import "The_Remember_ButtonsAppDelegate.h"
    
    When I try to call a method with the window in my delegate I get an error saying the window is undefined. What am I doing wrong? Can I not use objects from other classes?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Please post all of your code. A forward reference using @class will likely fix this, but we need to see more code.

    -Lee

    Edit: Re-reading I may have misunderstood, so more code would really help.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    Where or how is this additional class created? If it's created from your app delegate, you can add a method to pass the app delegate to the new class so that the new class has a reference to it.

    Alternatively, you could use [NSApp delegate] to access the controller object.

    You will then want to add a getter method/property for the window if there is not one already.
     
  4. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #4
    I created this new class by going to File>New File then I selected Obj-C with a subclass of NSObject. I want to use the window from my App Delegate. The window was declared like this:
    Code:
    
    NSWindow *window;
    
    
    I want to use this window in my new class, but for some reason a simple import won't work. It is probably something simple. Please Help
    Thanks
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    Show us the whole .h file - better yet, both of them.
     
  6. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    I would suspect that your "NSWindow *window;" is a instance variable for your delegate. Hence, other objects you create will not have a declared window variable EVEN IF you include the header for you delegate.

    Assuming your delegate object has appropriate @property, or otherwise declared accessor methods you should be able to access the window ivar as kainjow described, i.e., [[NSApp delegate] window]
     
  7. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #7
    Is there a keyword like "public" that makes ivars visible to other objects, like
    Code:
    @interface mySubclass : NSObject {
    public NSWindow *ownedWindow;
    }
    ?
    I have not used such a construct because the principles of object oriented programming suggest that you should avoid doing that just for consistency. I have only learned as much C as I need to get by.
     
  8. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #8
    Not so much, but even if there was you would still need a pointer to the instance of said object NOT just including the header of the class.

    Effectively, @property syntax is like the public keyword, but with more customization options.
     
  9. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #9
    Well, of course. Although you do have an instance of said object, there is no syntax for directly accessing its ivars in such a way.
     
  10. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #10
    Unless you made it a global variable or singleton/class accessor. :cool:
     
  11. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #11
    Although technically a global variable is by definition not an ivar. In the case of an object that serves as an app delegate, it typically would be because you can have only one actual app delegate, but there might be an unusual circumstance where the app delegate you define in the nib is just a launch manager that gets replaced by a different object to serve as the app delegate.

    Hopefully this is not confusing the OP too much, and possibly even being helpful.
     
  12. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #12
    Right I'm just pointing out ways to share an object without having a pointer to some other object to get it.

    Global variables should almost always be avoided, but since one app delegate or another will almost certainly be allocated/initialized only once and before most of the other objects, the app delegate could in theory setup a global variable pointer other objects or c code could reference at some later time.

    The other way one would be to be to use a class method to return the object, of which there are a couple of different ways to set that up. But none of them would rely on directly accessing an instance of the delegate, just the class object itself. (the [NSApp delegate] is an example of this class method/singleton pattern, for this specific example it would look like [MyAppDelegate window])

    Neither of these two options would be "easier" or "safer" than using a call like [[NSApp delegate] window]
     

Share This Page