2011-05-06 98 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,把它放在主)。除非你有需要在委託中聲明的理由嗎?

相關問題