2012-09-27 59 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

相关问题