1

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Osman
  • 1,771
  • 4
  • 24
  • 47

1 Answers1

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.

Community
  • 1
  • 1
Bejmax
  • 945
  • 7
  • 16
  • Sorry about the delay, but dosent this link them `[[self quoteHolder] setDelegate:self];` after I create it? – Osman Jan 03 '13 at 03:18
  • And if thats the case, lets say I am creating multipul of these, how would i do that? – Osman Jan 03 '13 at 03:26
  • 1
    @Mr.S The text field you have in your xib is a real object, just like the one you create with `alloc`/`initWithFrame:`, and it's already pointed to by the `IBOutlet` variable `quoteHolder`. Don't reassign that variable in `displayString`; just use the value that's already assigned when the xib is loaded. – jscs Jan 03 '13 at 06:24
  • @Mr.SirKingOsman if you were adding multiple `UITextField` 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](http://stackoverflow.com/a/3826990/1354100). – Bejmax Jan 03 '13 at 14:19