UIBarButtonItem: Change title at every tap

I want to make a UIBarButtonItem that changes title by tap.
I thought that this was nice and very easy but... I don't understand why action doesn't run.

Here my declaration:

    @IBOutlet weak var menuButton:UIBarButtonItem!

Then my code:

    @IBAction func change(_ sender: UIBarButtonItem) {
        if menuButton.title == "Website >" {
            //Do your stuff;
            menuButton.title = "< AppMenu"
        } else {
            //Do your stuff;
            menuButton.title = "Website >"
        }
    }

FYI, this may be a more appropriate question to ask on Stack Overflow, as it is not really about Swift, per se.

With that said, have you wired up the action from the storyboard to your change method?

This is wired as Sent action. But still not working.

let me send all the class. In a nutshell this app's part have a sidebar:

import UIKit


class Home: UIViewController {
   
  
@IBOutlet weak var lostconnection: UIImageView!

 @IBOutlet weak var menu: UIBarButtonItem!

@IBOutlet var Webview: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()
    //call sidebar
    menu.target = self.revealViewController()
    menu.action = #selector(SWRevealViewController.revealToggle(_:))
    //set sidebar normally opened
    UIApplication.shared.sendAction(menu.action!, to: menu.target, from: nil, for: nil)
    //set webview
    let homeurl = URL(string: "https://www.google.it")
    let homeurlrequest = URLRequest(url: homeurl!)
    Webview.loadRequest(homeurlrequest)
    
    //check connection
    if currentReachabilityStatus == .reachableViaWiFi {
        
    }else if currentReachabilityStatus == .reachableViaWWAN{
        
    } else {
        self.lostconnection.image = #imageLiteral(resourceName: "lostconnection.png")
    }
}

@IBAction func menutap(_ sender: UIBarButtonItem) {
    if menu.title == "< Appmenu" {
        menu.title = "website >"
    } else {
        menu.title = "< Appmenu"
    }
}




@IBAction func GoToLogintapped(_ sender: UIBarButtonItem) {
     self.performSegue(withIdentifier: "goToLogin", sender: self)
    
}


}

It seems that calling and setting sidebar normally opened, prevents the 'menutap' function to work

Before all, print something in menutap to see if it is being called. Make sure your UIBarButtonItem has a 'custom' identifier set.

that's weird... i maked 2 print in the if condition and they doesn't work.

You should be printing right away after entering the function. (what if the 'if' statement fails? You would still be wondering whether the function is called at all).

It is hard to tell what the problem is while everything's done in the interface builder and can't be properly shared as code. Please take some time to search for how to wire up your bar button to an @IBAction and make sure it is called. The rest will be easier.

My @IBAction is called. I yet controlled, but...
if I remove

menu.target = self.revealViewController()

and

menu.action = #selector(SWRevealViewController.revealToggle(_:))

The code works but without this two commands my sidebar can't work

Is menutap an @IBAction for menu? If so, this

will change the action. An @IBAction is nothing more than a way to specify a #selector through your storyboard, and a UIBarButtonItem obviously can't have more than one action (there is only one action). Move everything to a single function that would both check for the title and do what's in revealToggle(_:). By the way, the sender is the object that sends the action (menu), use it.

It works but only once