簡體   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