为什么我的可点击框需要双击才能获得等于1的点击次数? [英] Why does my clickable box needs a double click to have a clickcount that is equal to 1?

查看:130
本文介绍了为什么我的可点击框需要双击才能获得等于1的点击次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSStatusItem有一个NSMenuItem包含一个自定义NSView。这个NSView包含3个可点击的NSBox子视图(实现mouseDown事件)。奇怪的是,当我运行应用程序,我进入状态项后,我按下播放按钮,我必须点击NSBox只有一次(这返回clickCount = 1)。当我进入另一个窗口或视图,我回到statusItem,我试图单击一个NSBoxes没有什么发生。当我双击项目,操作发生,并且clickCount也等于1.

   - (void)mouseDown NSEvent *)theEvent {
NSLog(@ClickCount:%ld,theEvent.clickCount);
if([delegate respondingToSelector:@selector(boxClicked :)]){
[delegate boxClicked:self];
}
}

有没有人知道为什么会发生这种情况,我可以解决这个问题吗?



EDIT(完整的m文件代码):

  #importClickableBox.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 respondingToSelector:@selector(boxRolledOver :)]){
[delegate boxRolledOver:self];
}
}

- (void)mouseExited:(NSEvent *)theEvent {
if([delegate respondingToSelector:@selector(boxExited :)]){
[delegate boxExited:self];
}
}

- (void)mouseDown:(NSEvent *)theEvent {
NSLog(@ClickCount:%ld,theEvent.clickCount);
if([delegate respondingToSelector:@selector(boxClicked :)]){
[delegate boxClicked:self];
}
}

- (void)dealloc {
[delegate release];
[super dealloc]
}

@end


解决方案>

这可能是因为您的视图不是关键。你可以为你的NSMenu和 -menuWillOpen:设置一个委托,你可以调用 [boxView.window makeFirstResponder:boxView]; 让它成为钥匙。


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

解决方案

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.

这篇关于为什么我的可点击框需要双击才能获得等于1的点击次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆