繁体   English   中英

如何使用 Swift 更改 LaunchScreen 中的初始 ViewController?

[英]How can I change the initial ViewController in LaunchScreen with Swift?

我想检查用户是否登录,如果用户登录,则将他带到主屏幕,或显示欢迎屏幕。

您不能在 Launch Screen 中执行此操作,但您可以在 AppDelegate 的方法didFinishLauchingWithOption实现相同的功能,在那里您可以检查用户是否登录并设置根视图控制器并且不要在故事板中设置 initialViewController。 它应该是这样的

    NSString *identifier = isLoggedIn ? @"IdentifierForVC1" : @"IdentifierForVC2";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier: identifier];
    self.window.rootViewController = vc;

代码未在编辑器中测试可能有一些 Swift 代码应该是这样的

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier(identifier) as! UIViewController
self.window?.rootViewController = vc

迅速

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   self.window = UIWindow(frame: UIScreen.main.bounds)
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let viewController = storyboard.instantiateViewController(withIdentifier: "Identifier")
   let navigationController = UINavigationController(rootViewController: viewController)
   self.window?.rootViewController = navigationController
   self.window?.makeKeyAndVisible()

   return true
}

您不能在启动屏幕中执行此操作,但可以在 AppDelegate 中执行此操作

对于 Swift 4

 if userLoggedIn == True {
        //Do something 
        } else {
        //Do something
        }
        UserDefaults.standard.set(value:Bool  ,forKey:"loggedIn")  //This will save the bool value to your UserDefaults
      }


现在转到您的 AppDelegate


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

           self.window = UIWindow(frame: UIScreen.main.bounds)
            let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)


     if (UserDefaults.standard.bool(for key: "loggedIn")) == true {
         let welcomeVC = mainStoryboard.instantiateViewController(withIdentifier: "welcomeVC") as! WelcomeVC
            self.window?.rootViewController = newRootVC

            self.window?.makeKeyAndVisible()
        } else {
         let loginVC = mainStoryboard.instantiateViewController(withIdentifier: "loginVC") as! LoginVC
            self.window?.rootViewController = newRootVC

            self.window?.makeKeyAndVisible()
        }
return true
    }

快乐编码希望这会有所帮助:)

在 AppDelegate Swift 4.0 中编写此代码

didFinishLaunchingWithOptions将您的 viewController 传递给self.launch(rootController: ViewController())

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

    let frame = UIScreen.main.bounds
    self.window = UIWindow(frame: frame)
    self.window!.rootViewController = ViewController()
    self.window!.makeKeyAndVisible()
    return true
 }

在 AppDelegate Swift 5.0 Xcode 11.0 中编写此代码

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let _ = (scene as? UIWindowScene) else { return }

        if let windowScene = scene as? UIWindowScene{

            let window = UIWindow(windowScene: windowScene)
            let rootViewController = UIStoryboard(name: "Auth", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! UINavigationController

            window.rootViewController = rootViewController
            self.window = window
            window.makeKeyAndVisible()
        }

    } 

在 Swift 中,在你的 AppDelegate.swift 中,

self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("{INSERT_STORYBOARD_ID_HERE}") as? UIViewController

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM