T
T
Toly2016-12-28 14:59:27
Cocoa
Toly, 2016-12-28 14:59:27

Strange behavior of NSTokenField. How to fix?

I have a class FormatFieldwhich I have inherited from NSTokenField. The class FormatFieldhas a button to call NSMenu. It looks like this:
e5786d673cfa47d3b10604ba5eed36ca.png
When the plus sign (which is NSButton) is clicked, NSMenu appears:
63322c02d1fa410cae3ab248ef8d231b.png
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:
98e62914bbe04f98bea103c267bfbac9.png
When I hover over FormatFieldits 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 FormatFieldCellthat inherited from NSTokenFieldCellin 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

And as I wrote earlier, I created a class FormatFieldthat I inherited from NSTokenFieldto 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

I assign targets and actions to the button from the class where the work is processed 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 question

Ask a Question

731 491 924 answers to any question