It's much easier with Swift 4
or Swift 4.2
inside your ViewDidLoad
method, define your button and add it to the navigation bar.
override func viewDidLoad() {
super.viewDidLoad()
let logoutBarButtonItem = UIBarButtonItem(title: "Logout", style: .done, target: self, action: #selector(logoutUser))
self.navigationItem.rightBarButtonItem = logoutBarButtonItem
}
then you need to define the function that you mentioned inside action parameter as below
@objc func logoutUser(){
print("clicked")
}
You need to add the @objc
prefix as it's still making use of the legacy stuff (Objective C).