Reputation: 1771
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.
and there relavent content:
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
- (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
#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
#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?
Upvotes: 1
Views: 984
Reputation: 945
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.
Upvotes: 1