Is it possible to access parent object from its property?
In this simple application I have a window which responds to keypress events.
I want my property object named “window” to set its parent object “AppDelegate” variable “upKeyPressed” to a value when event occurs.
Is it possible in any way?
AppDelegate.h:
@interface MyWindow : NSWindow
@end
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
BOOL upKeyPressed;
}
@property (assign) IBOutlet MyWindow *window;
@end
AppDelegate.m file:
@implementation MyWindow
- (void)moveUp:(id)sender
{
// here I want to set upKeyPressed value to YES with a kind of:
self.parentObject->upKeyPressed = YES; // *** fantasy command
}
@end
@implementation AppDelegate
...
@end
No, there is no concept of parents when it comes to properties. What you want to do is add
@property (assign) AppDelegate *parentObject;(you can call it what you want) to yourMyWindowclass’s interface. Then synthesize it in your implementation.For it to work, you also need to set it to point to your app delegate – in your
-applicationDidFinishLaunchingWithOptions:addself.window.parentObject = self;. Then to access the so-called “parent” object from yourMyWindowinstance, just useself.parentObject.Edit: You need to forward-declare
AppDelegateby putting@class AppDelegate;before your interface definition forMyWindow.