mouseDown 不能在 NSTextField 上正确触发 [英] mouseDown not firing properly on NSTextField

查看:62
本文介绍了mouseDown 不能在 NSTextField 上正确触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实施这篇文章中发布的第二个答案 此处. 我希望作为提出问题的人,但是我的 mouseDown 无法正常工作/注册.这是我所拥有的.

I tried implementing the second answer posted in this post here. I have the desire as the person asking the question however my mouseDown is not working/registering. Here is what I have.

  1. AppDelegate.h
  2. AppDelegate.m
  3. MouseDownTextField.h
  4. MouseDownTextField.m

还有相关内容:

  1. AppDelegate.h

  1. AppDelegate.h

#import <Cocoa/Cocoa.h>
#import "MouseDownTextField.h"
@interface AppDelegate : NSObject <MouseDownTextFieldDelegate> {
NSWindow *window;
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
NSMutableArray *selector;
NSMutableArray *display;
NSTimer *timer;
MouseDownTextField *quoteHolder; }
@property IBOutlet MouseDownTextField *quoteHolder;
@end

  • AppDelegate.m

  • AppDelegate.m

    - (void)displayString:(NSString *)title {
      NSRect frame = NSMakeRect(50, 0, 200, 17);
      quoteHolder = [[MouseDownTextField alloc] initWithFrame:frame];
      [[self quoteHolder] setDelegate:self];
      [quoteHolder setStringValue:title];
      [quoteHolder setTextColor:[NSColor blueColor]];
      [test addSubview:quoteHolder];
      [statusItem setView:test]; }
      -(void)mouseDownTextFieldClicked:(MouseDownTextField *)textField {
         NSLog(@"Clicked");}
    

  • MouseDownTextField.h

  • MouseDownTextField.h

    #import <Appkit/Appkit.h>
    @class MouseDownTextField;
    
    @protocol MouseDownTextFieldDelegate <NSTextFieldDelegate>
    -(void) mouseDownTextFieldClicked:(MouseDownTextField *)textField;
    @end
    
    @interface MouseDownTextField: NSTextField {
    }
    @property(assign) id<MouseDownTextFieldDelegate> delegate;
    @end
    

  • MouseDownTextField.m

  • MouseDownTextField.m

    #import "MouseDownTextField.h"
    @implementation MouseDownTextField
     -(void)mouseDown:(NSEvent *)event {
    [self.delegate mouseDownTextFieldClicked:self]; }
    -(void)setDelegate:(id<MouseDownTextFieldDelegate>)delegate {
    [super setDelegate:delegate]; }
    -(id)delegate {
    return [super delegate]; }
    @end
    

  • 对可能出错或我做错了什么的想法?

    Thoughts on what could be wrong or what i have done wrong?

    推荐答案

    您正在 IB 中创建 quoteHolder,您应该删除以下代码行,您应该没问题.

    You are creating quoteHolder in IB, you should remove the following line of code and you should be fine.

    quoteHolder = [[MouseDownTextField alloc] initWithFrame:frame];
    

    重新分配 NSTextField 的结果是,您单击的不再是在委托中注册的那个.也不需要将其添加为 subview,它已经添加到 IB 中的视图层次结构中.

    The result of reassigning the NSTextField is that the one you are clicking is no longer the one registered with the delegate. No need to add it as a subview either, it's already been added to the view hierarchy in IB.

    另外,确保在 IB 中,在可访问性下,为 NSTextField 选中启用用户交互".

    Also, make sure in IB, under Accessibility, "User Interaction Enabled" is checked for the NSTextField.

    至于后续问题,你怎么会有多个这样的问题?

    As for the follow up quesion, how could you have multiple of these?

    如果您在 IB 中添加多个 NSTextField 实例,每个实例都将被引用为 @property,就像您对 quoteHolder 所做的一样.链接是在 IB 中完成的,就像这样链接答案.

    If you were adding multiple NSTextField instances in IB, each would be referenced as a @property just as you did with quoteHolder. The linkage is done in IB like this linked answer.

    这些都可以有相同的委托.当 mouseDownTextFieldClicked: 被按下时,你可以查询 NSTextField 的唯一 ID,它也可以在 IB 中分配.希望这会有所帮助.

    These could all have the same delegate. When mouseDownTextFieldClicked: is pressed you could interrogate the NSTextField for a unique id which could be assigned in IB as well. Hope this helps.

    这篇关于mouseDown 不能在 NSTextField 上正确触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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