So I\'m trying to change the left nav bar button item in viewWillAppear (the item needs to be changed back and forth, so viewDidLoad won\'t work). I have the following code in v
The following code should work:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let refreshButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: "buttonMethod")
navigationItem.leftBarButtonItem = refreshButton
navigationController?.navigationBar.barTintColor = UIColor.greenColor()
title = "Search result"
}
func buttonMethod() {
print("Perform action")
}
}
If you really need to perform it in viewWillAppear:
, here is the code:
import UIKit
class ViewController: UIViewController {
var isLoaded = false
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if !isLoaded {
let refreshButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: "buttonMethod")
navigationItem.leftBarButtonItem = refreshButton
isLoaded = true
navigationController?.navigationBar.barTintColor = UIColor.greenColor()
title = "Search result"
}
}
func buttonMethod() {
print("Perform action")
}
}
You can learn more about the navigationItem
properties with this previous question.