Search code examples
objective-cmacoscocoansmenunsmenuitem

How to set the number of displayed items of NSMenu?


As the title mentioned. I implemented an combo-box-like NSMenu object. But I wonder how to set the number of displayed items. Like the method of NSComboBox: -setNumberOfVisibleItems:

Could any one tell me?


Solution

  • There is no method built in. You could subclass NSMenu easily and override the addItem... and insertItem... methods to first check numberOfItems and remove or cancel as needed.

    Of course, if you are sure you will control that menu you can just do this checking before any coded that adds items.

    Of course you could also create a new delegate protocol that inherits from the NSMenuDelegate Protocol while you are at it and then easily have a delegate manage via methods like a shouldAddMenuItem or willAddMenuItem