Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中使用UserDefaults实现登录/注销导航?_Ios_Swift - Fatal编程技术网

Ios 如何在swift中使用UserDefaults实现登录/注销导航?

Ios 如何在swift中使用UserDefaults实现登录/注销导航?,ios,swift,Ios,Swift,****每次运行应用程序时,我都必须登录才能进入应用程序的主页 当我点击“登录”按钮直到点击“注销”按钮时,如何存储会话 因此,我可以避免每次运行应用程序时登录 请帮帮我 其他解决方案都不起作用 class LoginViewController: UIViewController { @IBOutlet weak var password: UITextField! @IBOutlet weak var loginName: UITextField! @IBActio

****每次运行应用程序时,我都必须登录才能进入应用程序的主页

当我点击“登录”按钮直到点击“注销”按钮时,如何存储会话

因此,我可以避免每次运行应用程序时登录

请帮帮我

其他解决方案都不起作用

class LoginViewController: UIViewController {

    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var loginName: UITextField!

   @IBAction func submitButton(_ sender: Any) {

  UserDefaults.standard.set(true, forKey: "isLoggedIn")

        if loginName.text == "test" && password.text == "test" {

            UserDefaults.standard.set(true, forKey: "status")
            Switcher.updateRootVC()

        }
        else{
            print("Invalid credentials")
        }
    }
}

我引用了此链接的相同方法,但它没有用,因为我没有得到预期的结果。
我是正确的状态代码,但不是正确的视图控制器。 请帮我解决这个问题


在给定的链接中,它将
TabbarVc
添加到Xcode 11中的tabor

,在我们创建新项目时,有2个文件,因此如果您想从代理文件更改rootViewController,则需要从SceneDelegate.swift文件加载该控制器

//SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }

    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    window?.windowScene = windowScene

    self.loadBaseController()
}


func loadBaseController() {
   let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
   guard let window = self.window else { return }
   window.makeKeyAndVisible()
   if UserDefaults.standard.bool(forKey: "isLoggedIn") == false {
       let loginVC: ViewController = storyboard.instantiateViewController(withIdentifier: "login") as! ViewController
       self.window?.rootViewController = loginVC
   } else {
       let homeVC: HomeViewController = storyboard.instantiateViewController(withIdentifier: "showData") as! HomeViewController
       let navigationHomeVC = UINavigationController(rootViewController: homeVC)
       self.window?.rootViewController = navigationHomeVC
   }
    self.window?.makeKeyAndVisible()
}

您必须使用SceneDelegate来解决此问题,如前一条评论所述。 但如果您仍然想使用Switcher类来实现这一点。 以下是解决方案:

在添加这些行之后。 现在编辑切换器类,如下所示:


现在,您可以使用switcher类从任何位置进行切换。

检查Appdelegate中的isLoggedIn值,并从Appdelegate文件中显示控制器。在Appdelegate文件中,要设置rootView控制器。确定。我会检查的我检查过了,但我不知道在哪里设置值
isLoggedIn
应用程序中的delegateComments不用于扩展讨论;此对话已结束。
SceneDelegate.swift
我没有此文件。。。。我的项目是在Xcode 11中创建的,上周我刚刚更新了我的Xcode检查了我更新的代码,我通过引用中间链接尝试了更新,但仍然没有得到答案……我可以在AppDelegate.swift中添加相同的代码吗,因为我是在Xcode 10中创建项目的。当我创建演示时,它对我有效当我在我的真实应用程序中尝试时,我的应用程序崩溃了
class AppDelegate: UIResponder, UIApplicationDelegate {


  var window: UIWindow?

    func loadBaseController() {
       let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
       guard let window = self.window else { return }
       window.makeKeyAndVisible()
       if UserDefaults.standard.bool(forKey: "isLoggedIn") == false {
           let loginVC: ViewController = storyboard.instantiateViewController(withIdentifier: "login") as! ViewController
           self.window?.rootViewController = loginVC
       } else {
           let homeVC: HomeViewController = storyboard.instantiateViewController(withIdentifier: "showData") as! HomeViewController
           let navigationHomeVC = UINavigationController(rootViewController: homeVC)
           self.window?.rootViewController = navigationHomeVC
       }
      self.window?.makeKeyAndVisible()
    }


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

       self.loadBaseController()

        return true

    }
}
//SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }

    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    window?.windowScene = windowScene

    self.loadBaseController()
}


func loadBaseController() {
   let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
   guard let window = self.window else { return }
   window.makeKeyAndVisible()
   if UserDefaults.standard.bool(forKey: "isLoggedIn") == false {
       let loginVC: ViewController = storyboard.instantiateViewController(withIdentifier: "login") as! ViewController
       self.window?.rootViewController = loginVC
   } else {
       let homeVC: HomeViewController = storyboard.instantiateViewController(withIdentifier: "showData") as! HomeViewController
       let navigationHomeVC = UINavigationController(rootViewController: homeVC)
       self.window?.rootViewController = navigationHomeVC
   }
    self.window?.makeKeyAndVisible()
}