how can hide particular button which is created from UIBarButtonItem in iphone?
how can hi开发者_StackOverflowde particular button which is created from UIBarButtonItem in iphone ?
I got warnings from XCode for both the setHidden and setAlpha calls when trying them, and sure enough they both crashed with unrecognised selector errors when executing.
The best answer i've found to showing / hiding UIBarButtonItems (via Harry Webster) is the following:
//hiding the button
self.UIBarButton.title = @"";
[self.UIBarButton setStyle: UIBarButtonItemStylePlain];
[self.UIBarButton setEnabled:false];
//showing the button
self.UIBarButton.title = @"Button Caption";
[self.UIBarButton setStyle: UIBarButtonItemStyleBordered];
[self.UIBarButton setEnabled:TRUE];
Hope this is of some use.
This seems to work best for me, and doesn't leave any empty space like the prior answer did:
NSArray *tbi = [[NSArray alloc] initWithObjects:ArchiveBtn,MoveBtn,DeleteBtn, nil];
self.toolbarItems = tbi;
Basically just set the toolbarItems to whatever you want. I have 6 buttons on the view in IB, all with IBOutlets setup, and then I just build different arrays for whatever buttons I want to be visible at any given time, and set self.toolbarItems to that array. Works for changing the order of the buttons as well.
An array of all UIBarButtonItems is stored in item
property of say UIToolbar.
In order to remove something just re-set this property:
// assuming you have
// @property (nonatomic, strong) IBOutlet UIToolbar* toolbar;
// and buttons Btn1, Btn2, Btn3 connected to appropriate properties in outlet
[toolbar setItems:[[NSArray alloc] initWithObjects:Btn1, Btn3, nil]];
[myUIBarButtonItem setHidden:YES];
or
[myUIBarButtonItem setAlpha:0.0];
精彩评论