ios - Xcode: Changing my UIBarButton item title if pressed? -
i trying set uibarbutton items title dependant on whether buttons been clicked or not, think close not sure why isn't working. using storyboard , barbuttons name/method "share1" , have is connected on storyboard, , heres code far:
in .h:
- (ibaction)share1:(id)sender;
and in .m:
#import "viewcontroller2.h" @interface viewcontroller2 () @property (assign, nonatomic) bool sharepressed; @end @implementation viewcontroller2 - (ibaction)share1:(id)sender { //i trying open menu in shouldn't affect button. if (self.sidemenu.isopen) { self.sharepressed = yes; [self.sidemenu close]; } else { [self.sidemenu open]; } self.navigationitem.rightbarbuttonitem = [[uibarbuttonitem alloc] initwithtitle:@"share" style:uibarbuttonitemstylebordered target:self action:@selector(share1:)]; } - (void)viewdidload { [super viewdidload]; if (_sharepressed) { self.navigationcontroller.navigationitem.rightbarbuttonitem = [[uibarbuttonitem alloc] initwithtitle:@"hide" style:uibarbuttonitemstylebordered target:self action:@selector(share1:)]; _sharepressed |= _sharepressed; } }
any appreciated.
first of all. may want use self.sharepressed
instead of _sharepressed
. use second way inside initializer, setter, , getter methods. , suggest use capital letters denote when new word in variable name begins. example, instead of sharepressed
use sharepressed
, makes easier read specially when name long.
now code. don't want allocate self.navigationitem.rightbarbuttonitem
again when action occurs. may want change title. better use this:
if using navigation controller
self.title = @"share";
if using navigation bar
self.navigationbar.title = @"share"; // navigationbar name gave object
Comments
Post a Comment