Search code examples
objective-cxcodetooltipnsviewnstextfield

NSControlls embedded in NSView do not show tooltips


I have multiple NSTextFields and other controls embedded in custom views:

Custom View
- Image View
- Custom View
-- Text Field
-- Text Field
.
.
.
- Custom View
-- Text Field
-- Text Field
.
.
.

NSTextFields have assigned tooltips. These tooltips are not being displayed, mouse events are probably intercept by NSView. Is there any way how to pass the events through the nsview or other way how to make ntextfields' tooltips to be displayed?


Solution

  • If you have an NSBox *box that contains several NSTextFields in it, issue this, which includes first binding to, or otherwise setting, the tool tip for your box view, and then designating the tool tip rectangle as the box view's frame with the box view as owner and sending it to the box view:

    [box bind:@"toolTip" toObject:myNSObjectControllerInstance withKeyPath:@"selection.something which is.my.box.tooltip.info.property.key.path" options:nil];
    [box addToolTipRect:box.frame owner:box userData:NULL];
    

    Hovering your mouse cursor over the text fields inside the box will show tool tips for the text fields. Hovering your mouse cursor over the box will show tool tips for the box... unless your mouse cursor is also inside (I guess the frame) of a nested subview of that box's view, and therefore the current tool tip level is already nested inside the box's view's view hierarchy:

    The normal behavior with tool tips is to only traverse inwardly into embedded views, not outwardly back up to the superviews. This could be your problem here, because one must by default move the mouse outside a view and then back inside a view in order to see tool tips of that view hierarchy level -- once your mouse hovers over a nested view, even if that nested view lacks a tool tip, tool tips for that nested view's superview will not display until you have moved the mouse cursor outside the superview and back into the superview, without also entering any of its subviews. Note that the tool tip's nested index does not reset after the tool tip has vanished.

    Maybe you can customize this with a subclass of NSView that has its own addToolTipRect:owner:userData: method. Check the toolTip-related Apple documentation and search The Internet for custom "toolTip" "NSView".