1

我试着解释一下情况:我有两个视图控制器:viewHome 和 viewStartTest。当学生第一次启动应用程序时,表中没有关于他的测试的任何数据。在这种情况下,应该在启动屏幕后显示 viewStartTest 控制器。但是当他再次启动应用程序并且条件“测试完成”为真时,viewHome 控制器应该在启动时显示。我尝试将此代码放入 AppDelegate.swift 并模拟完成的测试但仍然无法正常工作,感谢您的帮助:

// 0 - false, 1 - True
var conditionTest = 1

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

    if conditionTest == 1 {

        self.window = UIWindow(frame: UIScreen.main.bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewStartTest: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewStartTest")
        self.window?.rootViewController = viewStartTest
        self.window?.makeKeyAndVisible()

    } else {

        self.window = UIWindow(frame: UIScreen.main.bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let ViewHome: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewHome")
        self.window?.rootViewController = ViewHome
        self.window?.makeKeyAndVisible()
    }
}


在下面讨论后正确的场景委托代码:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
          if conditionTest == 1 {


        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewStartTest: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewStartTest")
        self.window?.rootViewController = viewStartTest
        self.window?.makeKeyAndVisible()

    } else {


        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let ViewHome: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewHome")
        self.window?.rootViewController = ViewHome
        self.window?.makeKeyAndVisible()
    }
}
        guard let _ = (scene as? UIWindowScene) else { return }
    }

4

1 回答 1

0

您需要存储conditionTest某处的值。我建议使用UserDefaults. 这是一个如何实现它的示例:

导航控制器:

class MainNavigationControllerViewController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()

    if isLoggedIn() {
        let homeController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC")
        viewControllers = [homeController]
    }
}

fileprivate func isLoggedIn() -> Bool {
    return UserDefaults.standard.isLoggedIn()
}
}

UserDefaults 的扩展:

extension UserDefaults {

func setIsLoggedIn(value: Bool) {
    set(value, forKey: "isLoggedIn")
    synchronize()
}

func isLoggedIn() -> Bool {
    return bool(forKey: "isLoggedIn")    }
}

如何使用:

使用上面的代码,您可以简单地登录/注销用户。一定要打电话.synchronize()

登录:

UserDefaults.standard.setIsLoggedIn(value: true)
UserDefaults.standard.synchronize()

登出:

UserDefaults.standard.setIsLoggedIn(value: false)
UserDefaults.standard.synchronize()
于 2020-04-01T21:15:05.517 回答