5

プログラムで閉じるボタンを追加したいと思いNSWindowます。ボタンを表示することはできますが、マウスオーバーまたはマウスダウンの効果はありません。ボタンをクリックしても、「セレクター」が呼び出されないようです。何が問題なのか、なぜこれがそんなに面倒なのか、よくわかりません。

これが私がいじっているものです:

closeButton = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:self.styleMask];

NSView *themeFrame = [[self contentView] superview];
NSRect c = [themeFrame frame];  // c for "container"
NSRect aV = [closeButton frame];    // aV for "accessory view"
NSRect newFrame = NSMakeRect(                                                c.size.width - aV.size.width - 5,  // x position                                                c.size.height - aV.size.height - 5,    // y position                                           aV.size.width,  // width                                                aV.size.height); // height

[closeButton setFrame:newFrame];    
[themeFrame addSubview:closeButton];
[closeButton setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];  
[closeButton setEnabled:YES];
[closeButton setTarget:self];
[closeButton setAction:NSSelectorFromString(@"testClick:") ];

「testClick」は私のクラスの単なるメンバー関数であり、次のように定義されています。

- (void)testClick:(id)sender

問題は次の呼び出しのようです:
[themeFrame addSubview:closeButton];

themeFrame は次のとおりです。[[self contentView] superview]ボタンを追加するだけで[self contentView]機能しますが、タイトルバーに追加したいと思います。

Interface Builder を使用しないでください...

4

1 に答える 1

0

潜在的な問題 # 1)

あなたの「NSSelectorFromString」の呼び方は、私には間違っているようです。Objective C でこの方法でパラメータを渡すことはできないと思います。

これを試して:

[closeButton setAction: @selector(closeWindow:)];

次のような新しい " closeWindow:" アクションを作成します。

- (void) closeWindow: (id) sender;

ウィンドウを閉じます。

潜在的な問題 # 2)

それ以外の:

closeButton = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:self.styleMask];
NSView *themeFrame = [[self contentView] superview];

使用しない理由:

NSWindow * parentWindow = [[self contentView] window];
if(parentWindow)
{
   closeButton = [parentWindow standardWindowButton:NSWindowCloseButton forStyleMask:self.styleMask];
}
于 2012-06-25T03:59:38.513 に答える