为什么我的可点击框需要双击才能使点击计数等于 1?

发布于 2024-12-17 17:30:55 字数 1420 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生死何惧 2024-12-24 17:30:55

听起来您确实还需要这样做:

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
  return YES;
}

以便您的自定义 NSView

可能还有其他问题,但无法从代码片段中看出。

It does sound like you also need to do this:

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
  return YES;
}

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.

时光瘦了 2024-12-24 17:30:55

这可能是因为您的观点不是关键。您可以为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文