I want to change UIBarButtonItem's title.
But this code is not working.
- (void)viewDidLoad {
...
[self smay];
}
- (void)smay {
...
AppDelegate *apd = (AppDelegate*)[[UIApplication sharedApplication]delegate];
NSString *SmayUse = [NSString stringWithFormat:#"%d月%d日",
apd.newyear,apd.newmonth];
[_smay setTitle:SmayUse]
}
I don't know how to solve this problem.
Please tell me a way to solve this problem.
Initialize your UIBarButtonItem like this:
_smay = [[UIBarButtonItem alloc] initWithTitle:#"Your Title"
style:UIBarButtonItemStyleBordered
target:self action:#selector(yourMethod)];
self.navigationItem.rightBarButtonItem = _smay;
Then, if you want to change the title somewhere else in your code:
[_smay setTitle:#"Your Other Title"];
IOS 8 and higher
UIBarButtonItemStyleBordered is deprecated, so use the below.
[[UIBarButtonItem alloc] initWithTitle:#"Your Title"
style:UIBarButtonItemStylePlain
target:self action:#selector(yourMethod)];
if u want to change the title of navigation bar programmatically means
UIBarButtonItem *aDoneItem = [UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered
target:self
action:#selector(toggleEdit:)];
navigationController.rightBarButtonItem = aDoneItem;
aDoneItem.title = #"Something";
You have to make sure the button is initialized. If it is part of a .xib file, make sure you have it linked up with your #property that has also an IBOutlet associated with it. Otherwise you need to initialize it programatically. Also, you should use the method setTitle:forState:.
item.title = #"new title" should work.
Related
I use this code to display a one item Toolbar in UIViewController (which is pushed by UINavigationController). The toolbar shows up and immediately disappear:
UIBarButtonItem * logoutButton = [[UIBarButtonItem alloc] initWithTitle:#"Log out" style:UIBarButtonItemStylePlain target:self action:nil];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
[self setToolbarItems:arr animated:NO];
[self.navigationController setToolbarHidden:NO animated:NO];
Do you know what the problem is?
where is this code? Doesn't seem to be anything out of the ordinary here so either has to do with where you are calling it, or something else that happens afterwards (like setting toolbarHidden to YES later or something)
I've programmatically created some UINavigationControllers and added them to a UITabBarController. Everything seems to work fine but I wanted to add a cancel button to the navigation controller but it never shows up. I've tried multiple ways but I can't seem to affect the display of the navigation items at all and I've followed multiple examples from here and other sites but nothing happens.
MyTableViewController *mtvc = [[MyTableViewController alloc] init];
UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:mtvc] autorelease];
myNavController.navigationBar.barStyle = UIBarStyleBlackOpaque; // this works
[mtvc release];
// TODO: figure out why added buttons aren't showing
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(shutDown)] autorelease];
myNavController.navigationItem.leftBarButtonItem = closeButton; // never shows up
I also tried adding the button this way
[myNavController.navigationItem setLeftBarButtonItem:closeButton animated:NO]; // also doesn't do anything
I started getting frustrated so I also tried some other things just to see if I could affect anything, but to no avail
myNavController.title = #"test"; // does nothing
I've tried doing it before and after the navigations controllers were added to the UITabBarController and that didn't help.
I've also tried rightBarButtonItem and tried using initWithTitle: instead of initWithBarButtonSystemItem.
Would someone please illuminate me? Clearly, I'm doing this the wrong way.
Try adding the bar buttons in the loadView method of MyTableViewController like the follwing.
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(shutDown)] autorelease];
self.navigationItem.leftBarButtonItem = closeButton;
I guess that should work.
Have you tried setting the button of the current view controller's navigation item like this:
mtvc.navigationItem.leftBarButtonItem = closeButton;
If you need to do that in Swift 3.0, it is simple like that:
let closeButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel,
target: self,
action: #selector({Your-viewController}.{close-method}))
self.navigationItem.leftBarButtonItem = closeButton
I need to change color on the default "self.navigationItem.backBarButtonItem". To accomplish this I´ve created an custom Button class and implemented it like this:
- (void)viewDidLoad {
[super viewDidLoad];
BackButton *blueSaveButton = [[BackButton alloc] init];
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:blueSaveButton];
temporaryBarButtonItem.title = #"Tillbaka";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
[blueSaveButton release];
[self gotoLocation];
}
But this has no effect at all on the button.
So how do you manage to do this without "breaking" the default/inherited behavior of the navigationbar?
EDIT: The reason is that the button needs to be corporate branded, so default styles will not do.
BR
If you want the button to be blue (like a Done button) and have the title "Save" you can use one of the built in bar button item types:
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(myAction:)] autorelease];
Ok found the solution (not so good but it works) do this:
1: Implement the button from this tutorial: www.switchonthecode.com
This will override the default behavior of navigationbar.
2: Implement this in every view that needs the custom button:
osmorphis.blogspot.com
BR
I am working on an iPhone's view which composed 3 elements, UITextView, UIToolBar with an UIBarButtonItem.
The goal is, I want UIBarButtonItem change its style from 'edit' (UIBarButtonSystemItemEdit) to 'Done' (UIBarButtonSystemItemDone) and update new selector to new method.
First of all, I have tried following code but it doesn't work:
Could you help me on this idea?
There is a builtin bar button with this behaviour, you get it via the editButtonItem property of a UIViewContoller. Tabbing that button will change the view controller it came from into editing mode, and toggle the button into a done button.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
If you have added the button through IB then make sure to set the identifier to Custom
Also allocate a button in the .h with appropriate IBOutlet and Property
Synthesize the button in .m
Then in your code do the following:
// Set to done
editButton.style = UIBarButtonItemStyleDone;
editButton.title = #"Done";
// Set back to edit
editButton.style = UIBarButtonItemStyleBordered;
editButton.title = #"Edit";
to change the button the Done button use this
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
to change the button to Edit button use this
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];
I ended up doing something like this. Unfortunately, setting the title directly did not work, for some reason it was nil and would not let me set it to a different value. The self.editButton comes from an IBOutlet with the target and actions set. This code uses ARC. I hope this helps someone.
NSString *title = app.settings.editing
? NSLocalizedString(#"Done", #"")
: NSLocalizedString(#"Edit", #"");
UIBarButtonItemStyle style = app.settings.editing
? UIBarButtonItemStyleDone
: UIBarButtonItemStyleBordered;
UIBarButtonItem *editButton
= [[UIBarButtonItem alloc] initWithTitle:title
style:style
target:self.editButton.target
action:self.editButton.action];
self.navigationItem.rightBarButtonItem = editButton;
UIBarButtonItem *rButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:NULL];
rButton.action = #selector(refreshAction);
self.navigationItem.rightBarButtonItem = rButton;
[rButton release];
The above code works fine to create a button and add it to the navigation bar, but when I click on the button it crashes the app with a EXC_BAD_ACCESS. If I comment out rButton.action = #selector(refreshAction); clicking the button won't do anything but it doesn't crash either.
- (void)refreshAction {
NSLog(#"refreshAction");
}
This code is in a TableViewController's viewDidLoad method which is pushed onto the navigationController stack from the NavigationViewController viewDidLoad method.
I've spent probably 3 hours trying to get this to work, to no avail.
As usual memory management was the culprit. Loading the tableViewController from the navigationController:
NearbyTableViewController *tableController = [[[NearbyTableViewController alloc] initWithNibName:#"NearbyTableViewController" bundle:nil] autorelease];
self.nearbyTableController = tableController;
[self pushViewController:self.nearbyTableController animated:YES];
[tableController release];
releasing an object set to autorelease... must be the most common error in memory management.
Deleting that line [tableController release] solved the problem
Any luck if you specify #selector(refreshAction) when you create the button, i.e.:
UIBarButtonItem *rButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshAction)];
Maybe the target doesn't get saved if you don't also specify the action to the initializer.
I'm not exactly 100% sure why your code does not work, but setting the selector directly in the constructor does work:
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refreshAction)];