In an app Im making, Im trying to enable login/logout to twitter. I want it so that there is a barbuttonitem that either says login if there is no current user, or logout if there is one. I created two buttons on the storyboard, one for login and one for logout. In the viewDidLoad(), I have this code:
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.rightBarButtonItem = nil
if twitterUser.currentUser != nil {
print("there is a current user")
self.navigationItem.rightBarButtonItem = logoutButton
} else {
self.navigationItem.rightBarButtonItem = loginButton
}
and it seems to work well. The problem comes when the user actually logs in or out. The buttons are connected to the following functions:
@IBAction func onLogin(sender: AnyObject) {
let client = twitterAPI.sharedInstance
client.login({ () -> () in
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.rightBarButtonItem = self.logoutButton
}) { (error: NSError) -> () in
print(error.localizedDescription)
}
}
@IBAction func onLogout(sender: AnyObject) {
twitterAPI.sharedInstance.logout()
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.rightBarButtonItem = self.loginButton
}
After logging in the login button will disappear, but the logout button does not show up. When logging out, the logout button disappears but the login button does not appear. I'm not sure what would be causing this, as I use the same methods in the viewDidLoad and it seems to work fine.
This code may help you