Answer the question
In order to leave comments, you need to log in
Strange behavior of NSTokenField. How to fix?
I have a class FormatField
which I have inherited from NSTokenField
. The class FormatField
has a button to call NSMenu. It looks like this:
When the plus sign (which is NSButton
) is clicked, NSMenu appears:
Everything seems to work fine, but when I call NSMenu by clicking on the plus sign, and then immediately click the mouse somewhere else in the program window so that the menu that appears disappears. The menu disappears, and with it the tokens and the text in the field disappear FormatField
:
When I hover over FormatField
its content, it appears, and when I move the cursor away, the content disappears - this behavior persists until I click on the field FormatField
.
Please help me figure out how to fix this strange behavior (could you provide code examples). Thank you all in advance.
To be clear:
I created a class FormatFieldCell
that inherited from NSTokenFieldCell
in order to rewrite the method drawingRectForBounds: to allocate space for the button in the fieldFormatField
@implementation FormatFieldCell
- (NSRect) drawingRectForBounds:(NSRect)rect {
NSRect newRect = [super drawingRectForBounds:rect];
CGFloat x = newRect.origin.x; //отступ сбоку 2
CGFloat y = newRect.origin.y; //отступ сверху 3
CGFloat width = newRect.size.width - 18; //ширина поля ввода (ширина TokenField за вычетом отступов и прочего по усмотрению) 22
CGFloat height = newRect.size.height; //высота поля ввода (высота TokenField за вычетом отступов и прочего по усмотрению) 4
NSLog(@"%s x: %f y: %f w: %f h: %f", __func__, x, y, width, height);
return CGRectMake(x, y, width, height);
}
@end
FormatField
that I inherited from NSTokenField
to add to it NSButton
:@implementation FormatField
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
_button = ;
[_button setBordered:NO];
[_button setBezelStyle:NSBezelStyleShadowlessSquare];
[self addSubview:_button];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
[self drawCell:self.cell];
NSLog(@"%s x: %f y: %f w: %f h: %f", __func__, dirtyRect.origin.x, dirtyRect.origin.y, dirtyRect.size.width, dirtyRect.size.height);
[_button setFrameOrigin:NSMakePoint(([self frame].size.width - 20), 1)];
}
@end
FormatField
. (These are several different classes):- (void)awakeFromNib {
tokens = [NSArray arrayWithObjects:[[Token alloc] initToken:@"number" withMenu:numberMenu], [[Token alloc] initToken:@"name" withMenu:nil], nil];
[formatField setObjectValue:tokens];
[[formatField button] setTag:69];
[[formatField button] setTarget:self];
[[formatField button] setAction:@selector(action:)];
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question