【问题标题】:Different view controller display at start app启动应用程序时显示不同的视图控制器
【发布时间】:2020-04-01 19:54:45
【问题描述】:

我试着解释一下情况: 我有两个视图控制器: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 }
    }

【问题讨论】:

  • 将此代码放入您的场景委托中。然后它工作。并在场景委托中使用窗口。
  • 您好,我试了一下,但启动屏幕后黑屏...
  • 你能分享场景代理吗?并检查您的视图控制器的标识符
  • @Kasim 我将其添加到问题中
  • 你可以使用 => var window: UIWindow?所以删除 self.window = UIWindow(frame: UIScreen.main.bounds)

标签: swift startapp


【解决方案1】:

您需要将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()

【讨论】:

    猜你喜欢
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2015-03-05
    相关资源
    最近更新 更多