setting a UIBarButtonItem to backBarButtonItem with an action, does not work - ios

When I assign the following UIBarButtonItem to backBarButtonItem I get a button with the title I assigned, but the action is never called.
Using leftBarButtonItem instead, will add a button and the action will be called. What is overriding my action? Can I do something about it?
override func viewDidLoad() {
super.viewDidLoad()
let backItem = UIBarButtonItem(title: "Back",
style: .plain,
target: self,
action: #selector(backNavigationClick))
navigationItem.backBarButtonItem = backItem
}
#objc func backNavigationClick() {
print("back clicked")
}

if you use it in this way you can customize the button.
You need to replace this code
navigationItem.backBarButtonItem = backItem
to
navigationItem.leftBarButtonItem = backItem
and add this code on backNavigationClick()
#objc func backNavigationClick() {
print("back clicked")
self.navigationController?.popViewController(animated: true) ***
}

Related

RightBarButtonItem not showing up, when leftBarButtonItem works fine

In my app I have a push segue from HomeViewController to EditProfileViewController which should have a back button as a leftBarItem and a settings cog as a rightBarItem. The back button displays normally, but the right item is missing. These ViewControllers live happen in the MainNavigationController which has a Navigation Bar.
I tried to define the rightBarButton in ViewDidLoad of the EditProfileVC, I also tried to have a rightBarItem in the storyboard for the View controller.
let buttonItem = UIBarButtonItem(image: settingsIcon, style: .plain, target: self, action: #selector(settingsPressed))
buttonItem.tintColor = UIColor(.settingsIconTint)
navigationItem.rightBarButtonItem = buttonItem
Interestingly if I change the rightBar to a leftBar item, the back button is replaced with the settings cog and works as I expect, but I can't go back to the main page.
let buttonItem = UIBarButtonItem(image: settingsIcon, style: .plain, target: self, action: #selector(settingsPressed))
buttonItem.tintColor = UIColor(.settingsIconTint)
navigationItem.leftBarButtonItem = buttonItem
To set a rightBarButtonItem in a navigationBar,
class HomeViewController: UIViewController {
#IBAction func openEditVC(_ sender: UIButton) {
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "EditProfileViewController") as? EditProfileViewController {
self.navigationController?.pushViewController(controller, animated: true)
}
}
}
class EditProfileViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let buttonItem = UIBarButtonItem(title: "Settings", style: .plain, target: self, action: #selector(settingsPressed))
buttonItem.tintColor = .red
navigationItem.rightBarButtonItem = buttonItem
}
#objc func settingsPressed() {
print("Setting Pressed")
}
}
In the above code I've added a UIBarButtonItem with title Settings as a rightBarButtonItem of navigationBar.
No need to configure leftBarButtonItem it not required. Back button is added by default.
Screenshot:
In case it doesn't satisfy your requirement, add a screenshot of what is expected so I can help.

How do I get the back bar button item to show up in my navigation item in iOS?

I am not able to show a back bar button item in my navigation item in my root view controller of my navigation controller.
I have tried setting different properties.
Other questions like this do not give me an answer that works.
Her is my code in my root view controller:
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
navigationItem.leftItemsSupplementBackButton = true
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Button", style: .plain, target: nil, action: nil)
navigationItem.setHidesBackButton(false, animated: true)
navigationItem.leftItemsSupplementBackButton = true
}
Assigning to leftBarButtonItem instead of backBarButtonItem works for me:
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Button", style: .plain, target: nil, action: nil)
}

Unable to add target and run function on UIBarButtonItem click in iOS Swift 3

I'm unable to detect on-click events on UIBarButtonItems in Swift 3. Can anyone help?. I need to perform a segue to another storyboard and view controller on clicking the UIBarButtonItem.
Swift 3.0:
override func viewDidLoad() {
super.viewDidLoad()
let controllerButton: UIBarButtonItem = UIBarButtonItem.init(title: "Next", style: .plain, target: self, action: #selector(ViewController.changeController))
self.navigationItem.rightBarButtonItem = controllerButton
}
func changeController() {
self.performSegue(withIdentifier: "Controller name segue", sender: AnyObject)
}
Please check this above code to for creating UIBarButtonItem and keep that on right navigation Item along with target and action.
Checkout sample code below:-
var b = UIBarButtonItem(
title: "Continue",
style: .plain,
target: self,
action: #selector(sayHello(sender:))
)
func sayHello(sender: UIBarButtonItem) {
}

Swift: Edit Mode, link editButtonItem() to IBAction

I understand how to set my UITableView into edit mode, and how to dynamically create an edit button:
override func viewDidLoad() {
tableView.allowsMultipleSelectionDuringEditing = true
tableView.setEditing(false, animated: false)
navigationItem.leftBarButtonItem = editButtonItem()
}
But when I tap the edit button, I would like a new button to appear on the navigation bar (i.e. a 'plus'/'add' button). To do this I think I need to create an IBAction, but I don't know how to link the editButtonItem() to an action. Any ideas?
Ok, big thanks to Ahmed and vadian for their comments, but what I got working was this:
override func setEditing(editing: Bool, animated: Bool) {
// Toggles the edit button state
super.setEditing(editing, animated: animated)
// Toggles the actual editing actions appearing on a table view
tableView.setEditing(editing, animated: true)
if (self.editing) {
navigationItem.rightBarButtonItem =
UIBarButtonItem(barButtonSystemItem: .Add, target: self,
action: #selector(clickMe))
} else {
// we're not in edit mode
let newButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
navigationItem.rightBarButtonItem = newButton
}
}
func clickMe()
{
print("Button Clicked")
}
As the edit button is pressed (and flips from Edit -> Done and back again) the code in the IF/ELSE statements will execute.
You can replace the default action of editButtonItem() by assigning a new function defined in your view controller to its action property.
editButtonItem().action = #selector(yourCustomAction(_:))
func yourCustomAction(sender: UIBarButtonItem) {}

performSegue with programmatically added Navigation button

I am trying to customize a Navigation bar button item programmatically... I am just having some issues here which I am sure is an easy fix. I want to make it the default Add button for now, but also in the future will want to make the bar button item a custom icon I create. so I guess I would like to know both ways how to programmatically change a navigation bar button to default styles and custom icons...thanks!
override func viewDidAppear(animated: Bool) {
let rightbutton = UIBarButtonItem(title: "+", style: UIBarButtonItemStyle.Plain, target: self, action: "uploadButtonClicked")
navigationItem.rightBarButtonItem = rightbutton
}
Here is how you would do it with the default add button:
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "uploadButtonClicked")
self.navigationItem.rightBarButtonItem = button
Here is how you would programmatically add a custom image:
let customImage = UIImage(named: "customButtonImage")
let customButton = UIBarButtonItem(image: customImage, style: UIBarButtonItemStyle.Plain, target: self, action: "uploadButtonClicked:")
self.navigationItem.rightBarButtonItem = customButton
To perform the segue, you can do this:
#IBAction func uploadButtonClicked(sender: UIBarButtonItem) {
performSegueWithIdentifier("segueIdentifier", sender: self)
}
Or, if you want to go to another storyboard from there, then your IBAction will look like this:
#IBAction func uploadButtonClicked(sender: UIBarButtonItem) {
let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
let nc = storyboard.instantiateInitialViewController() as! UINavigationController
let vc = nc.viewControllers.first as! YourViewController

Resources