On Friday, September 20, 2024 18:57 CEST, "H. Nikolaus Schaller" <[email protected]> wrote:
> Hi, > > > Am 20.09.2024 um 19:20 schrieb Sebastian Reitenbach > > <[email protected]>: > > > > Hi, > > > > I've a NSPopUpButton with some items. Some of them I want just to be > > disabled. > > To do so, I thought I can just get away with: > > > > [[self.popupButton itemWithTitle: @"Item 2"] setEnabled: NO]; > > > > but that is not the case. > > > > Is that supposted to work that way, if at all? If so, any hint what I'm > > doing wrong? > > I think (based on Cocoa experience) you must implement some validateMenuItem: > or similar method for the target of the popup button. So that it can return > YES or NO on demand. > > To make it dynamical I think the validateMenuItem: is passed the item in > discussion and you can make decisions based on e.g. [item action] or [item > title] (not recommended) or [item representedValue]. This is a little similar > to the dataSource of an NSTableView. You never set values directly in the > NSTableView but it asks some other object to provide them but only on demand > by whatever algorithm fits. > > This is called when a users opens the menus and depending on the result the > setEnabled: is called automatically (and overwrites your changes). relying on validateMenuItem: didn't worked out, since I couldn't properly check isEnabled: even though, the entry was disabled. As I understand now, how the validateMenuItem works is: the NSMenuItem has to have a action defined. Then when the Menu gets popuplated with items, and it's shown in the UI, the NSPopupButton, or someone sends a respondsToSelector: to the configured action: target class, and if that responds YES, the validateMenuItem: method is called in the target. This will mark all those itemas I want to be disabled as disabled, but I can't test, if the current selected item is disabled, and choose a different one in case it might be. However, looking again, I stumbled upon: NSPoUpButtons setAutoenablesItems: Once I set this to NO, I can use setEnabled: and isEnabled manually, and achieve what I was looking for. cheers, Sebastian > > BR, > Nikolaus >
