If you are using Swift in Xcode to develop an app. And you are using the navigation bar. You might find it difficult to change the bar button. So I got you a solution
Imagine that there is a navigation bar, and I want to change it from "Edit" to "Done", and "Done" to "Edit". So I can use the following code to do this
import UIKit
// Got to import the frameworks first
class ViewController: UIViewController
{
var isEdit: Bool = true
// Let's ignore the viewDidLoad function and the memoryDidRecieveWarning function and jump right into the IBAction
@IBAction func pressedNavButtonRight(sender: UIBarButtonItem) //Create an IBAction
{
if isEdit
{
isEdit = false
self.navigationItem.rightBarButtonItem! = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "pressedNavButtonRight:")
// For the title of the function above, set the title to the title you want to, the style is an enum, so you should get automatic suggestions when you type the dot syntax, and I believe there is plain, bordered, done, and something else. For target, write self. For action, inside a pair of quotation marks, write the function you want to call when the user press this button again. In this case, it is pressedNavButtonRight, but you have the add a ":" right after it.
}
else
{
isEdit = true
self.navigationItem.rightBarButtonItem! = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Bordered, target: self, action: "pressedNavButtonRight:")
}
}
//Code above
For further explanation, please check out the video
Imagine that there is a navigation bar, and I want to change it from "Edit" to "Done", and "Done" to "Edit". So I can use the following code to do this
import UIKit
// Got to import the frameworks first
class ViewController: UIViewController
{
var isEdit: Bool = true
// Let's ignore the viewDidLoad function and the memoryDidRecieveWarning function and jump right into the IBAction
@IBAction func pressedNavButtonRight(sender: UIBarButtonItem) //Create an IBAction
{
if isEdit
{
isEdit = false
self.navigationItem.rightBarButtonItem! = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "pressedNavButtonRight:")
// For the title of the function above, set the title to the title you want to, the style is an enum, so you should get automatic suggestions when you type the dot syntax, and I believe there is plain, bordered, done, and something else. For target, write self. For action, inside a pair of quotation marks, write the function you want to call when the user press this button again. In this case, it is pressedNavButtonRight, but you have the add a ":" right after it.
}
else
{
isEdit = true
self.navigationItem.rightBarButtonItem! = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Bordered, target: self, action: "pressedNavButtonRight:")
}
}
//Code above
For further explanation, please check out the video