标签栏控制器:显示登录控制器+停止双击标签按钮

时间:2016-10-26 00:31:41

标签: ios swift

我有一个带有UITabBarController的应用程序作为根视图。我不希望我的用户必须登录才能查看该应用程序,因此所有用户都可以看到第一个选项卡(选项卡1)。现在其他选项卡应该锁定在LoginViewController后面。

我在锁定视图中有以下内容:

ActivityViewController.swift

override func viewWillAppear(_ animated: Bool) {
   super.viewWillAppear(animated)
   if AccessToken.current != nil {
   // Do Stuff
   }
   else {
       let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "UserLoginViewController")
       self.navigationController?.pushViewController(vc, animated:false)
   }
}

UserLoginViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    navigationItem.hidesBackButton = true
    self.navigationController?.setNavigationBarHidden(true, animated: true)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if AccessToken.current != nil {
        _ = self.navigationController?.popViewController(animated: true)
    }
}

// Functions to handle login omitted
// I do the following once login is successful
_ = self.navigationController?.popViewController(animated: true)

// Close button to return to first no login required tab (Tab 1)
@IBAction func closeButtonPressed(_ sender: AnyObject) {
    // Send back to the first index in the tab bar controller
    self.navigationController?.tabBarController?.selectedIndex = 0
}

我有以下问题:

  1. 按一下锁定标签,可正确显示登录屏幕。再次按相同的选项卡,将关闭登录屏幕,为登录屏幕隐藏的视图设置动画,然后再次弹出登录屏幕。有没有办法阻止这个?

1 个答案:

答案 0 :(得分:0)

通常的做法是提供您的登录控制器UserLoginViewController,因此它会显示在UITabBarController前面。然后在登录控制器内部,在登录成功时添加解除操作

if AccessToken.current != nil {
    // Do Stuff
}
else {
    let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "UserLoginViewController")
    presentViewController(vc, animated: true, completion: nil)
}
相关问题