Can I have UIToolbar Bar Button Item States
Is there an开发者_如何学Python easy way to have an on state and an off state for the UIBarButtonItems, with different images for each?
Thanks
There isn't a built-in way, but I can think of a few approaches (depending on your needs):
- Bind the button to a method that toggles whatever the button is meant to toggle, and then changes the button's
image
property accordingly Create your own subclass of
UIBarButtonItem
that looks something like this:@interface ToggleBarButtonItem : UIBarButtonItem { BOOL _state; UIImage * onImage; UIImage * offImage; } - (BOOL)toggleState; @property (nonatomic, retain) UIImage * onImage; @property (nonatomic, retain) UIImage * offImage; @end @implementation ToggleBarButtonItem - (BOOL)toggleState { if (_state) { // Switch to Off state self.image = offImage; } else { // Switch to On state self.image = onImage; } return _state = !_state; } @end
精彩评论