2012-09-27 62 views
0

是否可以從其屬性訪問父對象?是否可以從其屬性訪問父對象?

在這個簡單的應用程序中,我有一個響應按鍵事件的窗口。 我希望我的名爲「window」的屬性對象在發生事件時將其父對象「AppDelegate」變量「upKeyPressed」設置爲一個值。 它有可能以任何方式?

AppDelegate.h:

@interface MyWindow : NSWindow 
@end 

@interface AppDelegate : NSObject <NSApplicationDelegate> 
{ 
    BOOL upKeyPressed; 
} 
@property (assign) IBOutlet MyWindow *window; 
@end 

AppDelegate.m文件:

@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 

回答

0

沒有,沒有父母的觀念,當談到性能。你想要做的是將@property (assign) AppDelegate *parentObject;(你可以稱之爲你想要的)添加到你的MyWindow類的界面。然後在你的實現中綜合它。

對於它的工作,您還需要將其設置爲指向您的應用程序代理 - 在您的-applicationDidFinishLaunchingWithOptions:中添加self.window.parentObject = self;。然後從MyWindow實例訪問所謂的「父」對象,只需使用self.parentObject即可。

編輯:您需要在MyWindow的接口定義之前加上@class AppDelegate;來轉發聲明AppDelegate

相關問題