I've created my toolBar and navigationBar and added the respective items to it. I'm still new to coding but its my understanding that UINavigationController only displays the navBar and toolBar for the viewController in the top of the seque. I'm doing this without storyboards and in swift.
If you want to add more than one bar button items to UINavigationBar programatically, you can do that like
UIBarButtonItem *btnShare = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(share)];
UIBarButtonItem *btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:btnShare, btnRefresh, nil]];
Hope this will help you.
I want to add a UIBarButton to the "navBar" of the viewController.
Here's my code:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:#"+ Contact"
style:UIBarButtonItemStylePlain
target:self
action:#selector(showPicker:)];
[self.navigationItem setLeftBarButtonItem:item animated:YES];
Nothing shows up on the "navBar". I also tried adding it through the storyboard but I couldn't connect it to the interface of my viewController as an IBAction.
Can't figure it out.
I am trying to add a UIBarButtonItem with the style of UIBarButtonSystemItemCompose. According to the apple docs it should display a compose icon that consists of a square outline. When I use the following code it just displays a red button. Does the icon only work if the uibarbuttonitem if placed inside of a UIToolBar and not a navigation bar.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:nil
style:UIBarButtonSystemItemCompose
target:self
action:#selector(tweetPressed:)] autorelease];
You are creating the button incorrectly. You need to use the proper init... method.
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(tweetPressed:)];
self.navigationItem.rightBarButtonItem = btn;
[btn release];
Look at the docs for the init... method you used. Look at what type should be passed for the style parameter and look at what the valid values are.
I want to bring up the .nib of "TableViewController" when a done button is clicked on my UIToolBar. But the below isn't allowing the click to bring up a new view. How do I rectify this? Please show me where I went wrong and what should be replaced and why.
//Here's the selector in my overlay.
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed)];
//Here's how I made my action. Btw, the uitoolbar has no nib, it's an overlay on the
//(camera mode).
-(void)doneButtonPressed {
TableViewController *tableView = [[TableViewController alloc]
initWithNibName:#"TableViewController" bundle:nil];
tableView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tableView animated:YES];
}
//Yet nothing happens when I click on my done button on my overlay. And I've made sure
// i've imported .h frameworks correctly too.
Suppose you were to bring up a nib from a barbuttonitem which is on a UItoolbar overlay. How would you do it?
I was told that to make it function properly I would have to add [barButtonItem addTarget:self action:#selector(doneButtonPressed) forControlEvents:UIControlEventTouchUpInside]; .
But if I add it I get this:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemDone addTarget:self action:#selector(doneButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
Which results in me getting an error reading "instance method' - initWithBarButtonSystemItem:target:action:forControlEvents:' not found (return type defaults to 'id')"
Instead of showing me only the correct additive, please show me the solution in addition to the code I've written here.
If you are using XCode 4, you can simply Ctrl+Drag the BarButtonItem, to your .h file, and you can create an IB Action from there automatically.
A UIBarButtonItem follows the default UIControlEventTouchUpInside and you cannot set it. Xcode should auto-suggest the methods for allocating it, but the correct code is:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed)];
Note, there's no forControlEvents:.
Try these changes:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
-(void)doneButtonPressed:(id)sender
{
}
the target is passed the object that initiated the action (i.e. the UIBarButtonItem itself)
Secondly, set a breakpoint in your doneButtonPressed function. If, for example, the tableView is getting set to nil, then you'll see nothing happen. i.e. perhaps there is an issue with instantiating this view controller.
By default the back button uses as a text on it a title of a viewcontroller.
Can I change text on the back button without changing a title of a view controller?
I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.
I tried the following which didn't work:
self.navigationItem.leftBarButtonItem.title = #"Back";
Thanks.
Try
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
I found that by looking at the backBarButtonItem docs in Apple's docs for UINavigationItem.
Marc W's approach worked great once I figured out which controller to apply it to: the one being re-titled, not the one on top. So if this is the navigation stack:
(bottom) ControllerA -> ControllerB (top)
...and you want to give a shorter title for ControllerA in the back-button displayed when ControllerB is on top, you apply the property change to ControllerA.
So it's more in the context of self.title, not like the other left/right-bar-button setters.
You can do it in the storyboard. Find the view controller you want to go back to (the one with the long title), select it's Navigation Item, and open the Attributes Inspector (Alt+Cmd+4), insert the custom Back Button title.
Thanks Marco... that helped me...
Here is what i did.
If you are using a tableView to navigate to different views... put the code:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
In your didSelectRowAtIndexPath method... of the first Controller... Controller A.
When you navigate to Controller B the button will have the title "Back".
The back button pulls its text from the title of the parent view controller.
In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.
For example, let's say we have a RootViewController class. When we click a cell in its table view, we push an instance of SecondViewController. We want the back button of the SecondViewController instance to read, "Home."
in the viewDidLoad method of RootViewController.m:
self.title = #"Home";
in the viewDidLoad method of SecondViewController.m:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
If you want your back button to read, "Back," set the title of the parent view controller to #"Back";
This work better for me. Try :
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
If you are using storyboard:
Open StoryBoard
In Document Outline window find ViewController to which you want to
return to
Click on Navigation Item of that ViewController
In Attributes explorer change Back Button value to your custom tile
That is it, enjoy...
And in MonoTouch the following works (in ViewDidLoad of the parent controller):
NavigationItem.BackBarButtonItem = new UIBarButtonItem( "Back", UIBarButtonItemStyle.Plain, null);
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc]
initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]];
This worked for me.
In your parent view controller, set the back button when view loads:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
}
Notice that we don't need to include autorelease at the end with the latest iOS version.
Hope this helps!
[self.navigationController.navigationBar.backItem setTitle:#"back"];
It works for me. You can replace "back" with something else.
This one worked for me if you don't want to have a title!
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
I finally knew why these answers did not work for me at first. I set the title in storyboard. When i set the title on code. it works!
self.navigationItem.title = #"Main Menu";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
My solution was to set title when the view controller is pushed to navigation stack and reset it by use of delegate method before pushed vc closes:
So I put the title change in calling view controller when I push the other view controller like:
self.pushedVC = [self.storyboard instantiateViewControllerWithIdentifier:#"pushedVCIdentifier"];
self.pushedVC.delegate = self;
[self.navigationController pushViewController:self.pushedVC animated:YES];
self.title = #"Back";
and in delegate callback function (which I invoke in viewWillDissapear):
-(void)pushedVCWillClose:(PushedVC *)sender
{
self.title = #"Previous Title";
}
If you want not only to change the text of the Back button and remain the original left-arrow shape, but also to do something when user clicks the Back button, I recommend you to have a look around my "CustomNavigationController".
//You can achieve this by setting the title in the previous view controller as shown below
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for this view
self.navigationItem.title = "My Title"
}
override func viewWillDisappear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for back button in next view
self.navigationItem.title = "Back"
}
For to change back button title, refer below code
InformationVC *infoController=[[InformationVC alloc]init];[self.navigationController infoController animated:YES];
//Below code changed back button title on InformationVC page.
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Information" style: UIBarButtonItemStylePlain target: nil action: nil];
self.navigationItem.backBarButtonItem = backBarButton;`enter code here`
In Swift5, the backBarButtom cannot be edited. Hence, we need to hide the backBarButtom first, then use a customised leftbarButtom to replace backBarButtom. Here is the detailed solution: https://stackoverflow.com/a/63868300/13939003
This worked for me:
self.navigationController.navigationBar.topItem.title = "Back"