BarButtonItem 未显示 [英] BarButtonItem not showing up

查看:26
本文介绍了BarButtonItem 未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我制作的一个应用程序中,我试图启用登录/退出推特.我想要它,以便有一个 barbuttonitem,如果没有当前用户,则表示登录,或者如果有则注销.我在情节提要上创建了两个按钮,一个用于登录,一个用于注销.在 viewDidLoad() 中,我有这个代码:

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
}

登录后登录按钮会消失,但注销按钮不会出现.注销时,注销按钮会消失,但不会出现登录按钮.我不确定是什么导致了这种情况,因为我在 viewDidLoad 中使用了相同的方法并且它似乎工作正常.

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

    override func viewDidLoad() 
    {
       super.viewDidLoad()
       barButton = UIBarButtonItem(title: "Login", style: UIBarButtonItemStyle.Done, target: self, action: "barBtnAction:")
       barButton.tag = 0
       self.navigationItem.rightBarButtonItem = barButton
    }

    @IBAction func barBtnAction(sender:UIBarButtonItem)
    {           
       let shouldLogin: Bool = sender.tag == 0 ? true : false
       if shouldLogin
       {
          //your code here
          onLogin()
       }
       else
       {
          //your code here
          onLogout()
       }
    }

  func onLogin()
  {
      barButton.title = "Logout"
      barButton.tag = 1
  }

  func onLogout()
  {
      barButton.title = "Login"
      barButton.tag = 0
  }

这篇关于BarButtonItem 未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆