Disabling tooltips globally

2019-08-10 05:32发布

Ok, I have used interface builder and added tooltips to all controls.

I would like to offer the user a menu item "disable tooltips".

How do I disable all tooltips globally on a cocoa application?

1条回答
The star\"
2楼-- · 2019-08-10 06:26

Instead of setting the text for the tooltips in directly in Interface Builder, make NSString properties for them in your view controller (or other bindable object). Use a Boolean property to control whether or not the tooltips will be shown.

@interface YourViewController : NSViewController

@property (readonly) NSString *thisTooltip;
@property (readonly) NSString *thatTooltip;

@property BOOL showTooltips;

@end



@implementation YourViewController

- (NSString *)thisTooltip {

   if (showTooltips) {
      return @"This is a tooltip";
   }
   else return @"";
}

- (NSString *)thatTooltip {

   if (showTooltips) {
      return @"That is a tooltip";
   }
   else return @"";
}

@end

Use the Bindings Inspector in IB to bind the Tooltip to the Property:

enter image description here

As you can see, this strategy makes it possible to customize your tooltips dynamically, while your application is running.

查看更多
登录 后发表回答