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

import UIKit

class TabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

图像:
ios - 如何将UITabBarController导航到ViewController?-LMLPHP
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我试过上面的代码它不工作

最佳答案

嘿,我刚刚创建了一个示例,它将帮助您:-
我只是用userdefaults来保存loginState
这里是:-
1)。任务视图控制器
我刚在里面创建了一个注销按钮

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

}



}

(第二章)。attentidanceviewcontroller-我没有在里面做任何东西
import UIKit

class AttendanceViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

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


}

这两个控制器都在TabBarController内,即HomeController
import UIKit

class HomeController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()


}

}

我在Main.storyboard文件中创建了它:-
ios - 如何将UITabBarController导航到ViewController?-LMLPHP
以及在login.storyboard文件中创建的LoginViewController
ios - 如何将UITabBarController导航到ViewController?-LMLPHP
LoginViewController:-此处保存了loginState
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检查didFinishLaunchingWithOptions中的loginState,如下所示:-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let userLoginStatus = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
    print(userLoginStatus)

    if(userLoginStatus)
    {
        let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
        let centerVC = mainStoryBoard.instantiateViewController(withIdentifier: "HomeController") as! HomeController
        window!.rootViewController = centerVC
        window!.makeKeyAndVisible()
    } else {

        let mainStoryBoard = UIStoryboard(name: "Login", bundle: nil)
        let centerVC = mainStoryBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
        window!.rootViewController = centerVC
        window!.makeKeyAndVisible()
    }

    return true
}

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

关于ios - 如何将UITabBarController导航到ViewController?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53496287/

10-12 00:16