2011-05-06 97 views
0

如果我将这些行放在xxxAppDelegate.m文件中,它工作正常。但我需要在另一个模块(如main.m等)下使用它。编译器生成一个错误,指出该窗口未定义。 “窗口”,在xxxAppDelegate modues定义,你怎么引用它在另一个模块xxxAppDelegate.m在运行时在objective-c中添加按钮控件Cocoa

旁边
NSView *superview = [window contentView]; 
    NSButton *button = [ [ NSButton alloc ] initWithFrame: NSMakeRect(10.0, 10.0, 200.0, 100.0) ]; 
    [ button setBezelStyle:NSRoundedBezelStyle]; 
    [ button setTitle: @"Click" ]; 
    [superview addSubview:button]; 
    [button setTarget:self]; 
    [ button setAction:@selector(doSomething:)]; 

回答

1

可可喜欢保持东西的模块化。窗口不存在于委托的上下文中,因为它是另一个类。

作出window属性在你的应用程序代理,如果它不存在:

.H

@property(readonly)NSWindow * window; 

.M

@synthesize window; 

则:

((YourDelegateClass *)[NSApp delegate]).window应工作。

+0

谢谢!那样做了。虽然我意识到创建一个类的实例的一般问题......但我可以弄清楚确切的语法......好的工作! – user523234 2011-05-07 01:36:04

0

你可以只粘贴代码,无论你需要它(IE,把它放在主)。除非你有需要在委托中声明的理由吗?

相关问题