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

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
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?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
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
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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]
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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]

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.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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.

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.
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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.

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.
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
Unless you made it a global variable or singleton/class accessor. :cool:

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.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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.

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]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.