Search code examples
objective-cmacosnspopupbutton

nspopup incorrectly logs numberOfItems at 0


I have an NSPopUpButton that is populated with an arraycontroller. When I run an nslog to ask [popup numberOfItems] it prints 0, even though when the program runs, I'm looking at 3 menu items populated in the popup. So the log must be printing before the menu is populated.

When does NSPopUpMenu populate, and how can I change the time at which that is done?


Solution

  • Figured it out. Even though "automatically prepares content" was checked, that is delayed until after awakeFromNib and windowControllerDidLoadNib:load.

    as per apple docs:

    This can create a problem if you want to perform an operation with the contents of an object controller in either of these methods, since the controller's content is nil. You can work around this by executing the fetch "manually" with fetchWithRequest:merge:error:.