为什么我的可点击框需要双击才能使点击计数等于 1?
我有一个 NSStatusItem,它有一个 NSMenuItem,其中包含一个自定义 NSView。这个 NSView 包含 3 个可点击的 NSBox 子视图(实现了 mouseDown 事件)。奇怪的是,当我运行应用程序并在按下“播放”按钮后立即输入 statusItem 时,我必须单击 NSBox 一次(这将返回 clickCount = 1)。当我进入另一个窗口或视图并返回到 statusItem 并尝试单击其中一个 NSBox 时,什么也没有发生。当我双击这些项目时,会发生操作,并且 clickCount 也等于 1。
-(void)mouseDown:(NSEvent *)theEvent {
NSLog(@"ClickCount: %ld", theEvent.clickCount);
if ([delegate respondsToSelector:@selector(boxClicked:)]) {
[delegate boxClicked:self];
}
}
有谁知道为什么会发生这种情况以及如何解决这个问题?
编辑(完整的m文件代码):
#import "ClickableBox.h"
@implementation ClickableBox
@synthesize delegate;
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}
- (void)viewDidMoveToWindow {
[self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:NO];
}
-(void)mouseEntered:(NSEvent *)theEvent {
if ([delegate respondsToSelector:@selector(boxRolledOver:)]) {
[delegate boxRolledOver:self];
}
}
-(void)mouseExited:(NSEvent *)theEvent {
if ([delegate respondsToSelector:@selector(boxExited:)]) {
[delegate boxExited:self];
}
}
-(void)mouseDown:(NSEvent *)theEvent {
NSLog(@"ClickCount: %ld", theEvent.clickCount);
if ([delegate respondsToSelector:@selector(boxClicked:)]) {
[delegate boxClicked:self];
}
}
- (void)dealloc {
[delegate release];
[super dealloc];
}
@end
I have an NSStatusItem that has an NSMenuItem which contains a custom NSView. this NSView contains 3 Subviews of NSBox which are clickable (implemented the mouseDown event). Strangely, when I run the application and I enter the statusItem right after I pressed the 'Play'-Button I have to click the NSBox just one time (this returns clickCount = 1). When I enter another window or view and I go back to the statusItem and I try to click one of the NSBoxes nothing happens. When I double click the items, the actions happen and the clickCount is also equal to 1.
-(void)mouseDown:(NSEvent *)theEvent {
NSLog(@"ClickCount: %ld", theEvent.clickCount);
if ([delegate respondsToSelector:@selector(boxClicked:)]) {
[delegate boxClicked:self];
}
}
Does anyone have any idea why this is happening and how I can solve this?
EDIT (full m-file code):
#import "ClickableBox.h"
@implementation ClickableBox
@synthesize delegate;
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}
- (void)viewDidMoveToWindow {
[self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:NO];
}
-(void)mouseEntered:(NSEvent *)theEvent {
if ([delegate respondsToSelector:@selector(boxRolledOver:)]) {
[delegate boxRolledOver:self];
}
}
-(void)mouseExited:(NSEvent *)theEvent {
if ([delegate respondsToSelector:@selector(boxExited:)]) {
[delegate boxExited:self];
}
}
-(void)mouseDown:(NSEvent *)theEvent {
NSLog(@"ClickCount: %ld", theEvent.clickCount);
if ([delegate respondsToSelector:@selector(boxClicked:)]) {
[delegate boxClicked:self];
}
}
- (void)dealloc {
[delegate release];
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您确实还需要这样做:
以便您的自定义
NSView
。可能还有其他问题,但无法从代码片段中看出。
It does sound like you also need to do this:
so that first mouse-click events are accepted in your custom
NSView
.There may be other issues, but can't tell from the code-snippet as-is.
这可能是因为您的观点不是关键。您可以为 NSMenu 设置委托,并在
-menuWillOpen:
上调用[boxView.window makeFirstResponder:boxView];
使其成为关键。This could be because your view is not key. You can set a delegate for your NSMenu and on
-menuWillOpen:
you can call[boxView.window makeFirstResponder:boxView];
to make it key.