mouseDown not firing properly on NSTextField

五迷三道 提交于 2020-01-05 06:51:31

问题


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

and there relavent content:

  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
    
  2. 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");}
    
  3. 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
    
  4. 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?


回答1:


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

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

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.

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?

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.

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.



来源:https://stackoverflow.com/questions/14132264/mousedown-not-firing-properly-on-nstextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!