如何将UITabBarController导航到ViewController?

时间:2018-11-27 09:19:03

标签: ios swift google-cloud-firestore

我里面有一个TabBarController,正在加载两个ViewControllers

import UIKit

class TabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

图片:

enter image description here

class AttendanceViewController: UIViewController,UIApplicationDelegate,
    UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate,
    CLLocationManagerDelegate,UIImagePickerControllerDelegate {

    override func viewWillAppear(_ animated: Bool) {
    if  Auth.auth().currentUser?.uid == nil {
        //Below code is not working
        let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main)
        .instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
        self.navigationController?.pushViewController(vc!, animated: true)
        print("user is not login in redirect to login page")
        return
    }else{
        print("User Successfully loginedin")
    }
}

}
  

注意::我有两个视图控制器,例如 AttendanceViewController TaskViewController ,我想检查用户是否未登录,然后重定向到我的    LoginViewController 我在上面的代码中尝试了它不起作用

4 个答案:

答案 0 :(得分:1)

当前的UIViewController

如果您的ViewController嵌入在UINavigationController中,则您编写的代码会起作用,但实际上不是。因此,除了显示UIViewController之外的UINavigationController之外,还可以从LoginViewController推入。因此,将其替换:

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main)
.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
self.navigationController?.pushViewController(vc!, animated: true)

使用

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main)
.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
present(vc!, animated: true, completion: nil)  

嵌入UINavigationController中

或者,您也可以将UIViewController嵌入UINavigationController中,并使用标识符设置segue,然后对LoginViewController执行此segue。

enter image description here

enter image description here

enter image description here

7。进行搜寻

performSegue(withIdentifier: "YourIdentifier", sender: self)

答案 1 :(得分:1)

嘿,我刚刚创建了一个示例,它将为您提供帮助:-

我只是使用userdefaults保存loginState

这里是:-

1)。 TaskViewController

我刚刚在其中创建了一个退出按钮

import UIKit

class TaskViewController: UIViewController {


@IBOutlet weak var signOutButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func viewDidLayoutSubviews() {
    signOutButton.layer.cornerRadius = signOutButton.layer.frame.height/2
}



@IBAction func signOutButtonTapped(_ sender: Any) {

    UserDefaults.standard.set(false, forKey: "isUserLoggedIn")  // here I remove the boolean value
    UserDefaults.standard.synchronize()
    let storyBoard = UIStoryboard(name: "Login", bundle: nil)
    let loginVc = storyBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
    let rootVc = UINavigationController(rootViewController: loginVc)
    UIApplication.shared.keyWindow?.rootViewController = rootVc

}



}

2)。 AttendanceViewController -我没有在其中创建任何内容

import UIKit

class AttendanceViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}


}

这两个控制器都位于TabBarController的{​​{1}}内部

HomeController

我在import UIKit class HomeController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() } } 文件中创建了它: enter image description here

Main.storyboard文件中创建了LoginViewController enter image description here

LoginViewController:-在这里保存了loginState

login.storyboard

最后在import UIKit class LoginViewController: UIViewController { @IBOutlet weak var loginButton: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func viewDidLayoutSubviews() { loginButton.layer.cornerRadius = loginButton.layer.frame.height/2 } @IBAction func loginButtonTapped(_ sender: Any) { let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let viewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeController") as! HomeController UserDefaults.standard.set(true, forKey: "isUserLoggedIn") UserDefaults.standard.synchronize() UIApplication.shared.keyWindow?.rootViewController = viewController } } 中像这样检查Appdelegate.swift中的loginState:-

didFinishLaunchingWithOptions

这是完整的项目-https://www.dropbox.com/s/3d34raovyx6orda/saveLogin.zip?dl=0

答案 2 :(得分:0)

我认为您想在appDelegate中进行检查。如今,我也正在使用UITabbarcontroller。我建议设置一个标签(使用userDefaults)来检查用户是否登录。我的代码是

class AppDelegate: UIResponder, UIApplicationDelegate , UITabBarControllerDelegate , GIDSignInDelegate{

    var window: UIWindow?

    var userCurrentLocation : CLLocation?
    var mapView : GMSMapView?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        if UserDefaults.standard.value(forKey: "LoginStatus")as? String == "1"
        {
            let storyBoard = UIStoryboard(name: "Main", bundle: nil)
            let initailVC = storyBoard.instantiateViewController(withIdentifier: "tabBar")
            let navVC = UINavigationController(rootViewController: initailVC)
            self.window?.rootViewController = navVC
        }

        return true
    }

我正在使用此代码检查用户是否正在登录,它将重定向到TabBarController,否则将保留在LoginVC中。

答案 3 :(得分:0)

  • 将您的UITabBarController嵌入UINavigationController

    UINavigationController(rootViewController: UITabBarController(nibName: "TabBarNibName", bundle: Bundle.main))

  • 只需从TabBarController的导航控制器中推送

    tabbarcontroller.navigationController?.pushViewController(vc!, animated: true)