0

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?

Andrew Chang
  • 1,289
  • 1
  • 18
  • 38
  • @CodaFi Which method? – Andrew Chang Feb 26 '14 at 05:34
  • I'm confused - you say you implemented a combo-box-like `NSMenu` object, but you're aware of `NSComboBox`. Why didn't you just use `NSComboBox`? Or at the least, why didn't you subclass it rather than trying to make `NSMenu` into a combo box? – user1118321 Feb 26 '14 at 05:45
  • That doesn't matter. It's not about why here but how. – uchuugaka Feb 26 '14 at 05:49
  • @user1118321 I want to make a customized combo box, but told that I cannot. Instead I could use popover or NSMenu to achieve that ([HERE](http://stackoverflow.com/questions/21361766/any-example-about-custom-nscombobox)). Then I make a customized combo box like [THIS](http://stackoverflow.com/questions/22030062/cannot-track-mouse-enter-exit-in-custom-nsmenuitem). But I do not how to limit the number of displayed items... Sorry for the confusing. – Andrew Chang Feb 26 '14 at 05:58

1 Answers1

1

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

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
  • Thank you! So, the answer is "no"... As to make my own list with limited displayed items, I tried to make all items in one view and handle mouse events myself. But I found I cannot track mouse in/out events: ([My other question](http://stackoverflow.com/questions/22030062/cannot-track-mouse-enter-exit-in-custom-nsmenuitem)). Do you know the solution? – Andrew Chang Feb 26 '14 at 13:21