Uli's Web Site |
|
blog |
Taking advantage of validateUserInterfaceItem
Most of you will already know how convenient it is to use -validateMenuItem: and -validateToolbarItem: in your controller to make sure the toolbar/menu items are accurately enabled. These methods also let you perform some more accurate validation than simply "first responder provides that function"; for example, you can check whether there is anything selected and disable the "clear" menu item for your custom view. One of the annoyances of this approach is that usually toolbar items duplicate items that already exist in the menus, forcing you to write a dummy-validateToolbarItem that just calls through to validateMenuItem. But there's an easier way than that: -(void) updateUI { NSWindow* win = [self window]; NSView* cView = [win contentView]; NSEnumerator* enny = [[cView subviews] objectEnumerator]; NSView* sView = nil; while( (sView = [enny nextObject]) ) { if( [sView isKindOfClass: [NSButton class]] ) { [(NSButton*)sView setEnabled: [self validateUserInterfaceItem: sView]]; } } }This loops over the buttons in your window (top level only -- deep search is left as an exercise to the reader) and calls -validateUserInterfaceItem: on each of them, and uses the result to enable/disable them. Every time you do something that may need a change in the UI, call [self updateUI] and all your buttons will refresh correctly. Instant User Interface Feedback! While this may be too generic and perform badly for very complex GUIs, it's very handy for creating the initial version of a GUI and for smaller apps. Moreover, you can easily move items between buttons, toolbars and menus without the need to change your code. We're one step closer to the Model-View-Controller ideal. And that without relying on any 10.3- or 10.4-specific features. |
Created: 2005-06-09 @574 Last change: 2005-06-09 @589 | Home | Admin | Edit © Copyright 2003-2024 by M. Uli Kusterer, all rights reserved. |