I know this question has been asked before and I've found two different lines of code that purport to perform a change in the identifier for a bar button item in a navigation bar. Both compile, but neither has any effect on the identifier. I start the program with the identifier set to a Play button, and want to change it to a Pause button. I've run these two lines of code both in viewDidLoad() and inside
IBAction func startButton(sender: AnyObject) {
Here is the code. Can anyone tell me why the button identifier is not changing? And, is the rightButton/leftButton as obvious as it appears, or is there something about words in the code I don't get.
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "startButton:")
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "startButton"), animated: true)
Here is the entire viewDidLoad, where it does not work either.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "startButton:")
}
Try below code to toggle button in UINavigationBar
In override func viewDidLoad() writes
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Play, target: self, action:Selector("startButton:"))
and create two #IBAction for button
#IBAction func startButton(sender: UIButton!) {
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "stopButton:"), animated: true)
}
#IBAction func stopButton(sender: UIButton!) {
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "startButton:"), animated: true)
}
#Bendrix, you can see code which I have tried my view controller code
And video after run this code in simulator app in simulator
Related
I have two buttons inside of a toolbar that is positioned at the bottom of the screen. I would like to center those buttons within the toolbar. Everything I have found is focused on centering buttons within a nav bar and I am unsure of how to approach this issue. What am I missing?
This is the UI I am attempting to emulate:
Adding a flexible space to the left and right of the buttons centered the buttons inside of the toolbar. Thanks to Midhun MP for the tip.
Thks #Andrew, it works
but for someone who wants it from code here is the example in Swift 5.0
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setToolbarHidden(false, animated: true)
let spaceItemLeft = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let nextItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(nextTapped))
let spaceItemRight = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbarItems = [spaceItemLeft, nextItem, spaceItemRight]
}
#objc func nextTapped() {
print("NEXT Tapped")
}
I have a Navigation controller and I'm trying to put a button on the right of navigation bar but I can't handle the tap action. I'm declaring the UIBarButtonItem like this
let navigationButton = UIBarButtonItem.init(title: "Logout", style: .done, target: self, action: #selector(RestaurantsListViewController.logoutAction))
I'm adding the button on the viewDidLoad func
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = navigationButton
}
and the function that I'm trying to use to handle the tap event is this
func logoutAction(sender: AnyObject?){
print("Logout")
}
but when I press the button, the message is not printed in console.
Try this:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "ButtonName", style: .done, target: self, action: #selector(YourViewController.yourAction))
}
let okbtn = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.logoutAction))
Try This
otherwise You need to replace func like
func logoutAction()
{
print("logout")
}
The issue here is when you create navigationButton as a class property, it gets initialized before self is initialized. So self doesn't exist yet when you pass it in as the target of the button.
There are a couple ways to fix it, including the answer by #Mannopson where you initialize the button inside the viewDidLoad, which ensures that self has already been created.
Another way to solve this issue is to declare navigationButton to be a lazy var:
lazy var navigationButton = UIBarButtonItem.init(title: "Logout", style: .done, target: self, action: #selector(RestaurantsListViewController.logoutAction))
The lazy var ensures that the property only gets initialized when the property gets accessed (hence it being a lazy initialization). Since the first time it is accessed happens in viewDidLoad, we can also be sure that self has been created. Hope this gives you more context!
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) {
}
I have created
NavigationController(Main) - > LoginViewController -> Tabbarviewcontroller -> HomeViewController
If I add barbutton item in HomeViewController through storyboard it's not displaying in simulator.
But I can see the changes in storyboard.
Title Home1 and barbutton item not displaying in simulator
Try this Code: Tested in Swift 3:
Note: Delete all your barButtonItems and try below code.
Add this code to your Home1 VC:
override func viewWillAppear(_ animated: Bool) {
let RightButtomitem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(handler))
self.tabBarController?.navigationItem.rightBarButtonItem = RightButtomitem
}
func handler(sender:UIButton) {
print("Add Button pressed")
}
Add this code to your Home2 VC:
override func viewWillAppear(_ animated: Bool) {
let RightButtomitem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handler))
self.tabBarController?.navigationItem.rightBarButtonItem = RightButtomitem
}
func handler(sender:UIButton) {
print("Done Button Pressed")
}
Output:
First you hide Navigation Bar in NavagationController and you make custome bar After you Add in it which you want in bar .
You can try it programmatically
HomeViewController ---
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
let barButtomitem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(handler))
self.tabBarController?.navigationItem.rightBarButtonItem = barButtomitem
}
SecondItemViewController--
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
self.tabBarController?.navigationItem.rightBarButtonItem = nil
}
and don't add barbutton in storyboard ...
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) {}