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

问题描述 投票:2回答:5

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

ios swift viewcontroller
5个回答
6
投票

您无法在启动屏幕中执行,但您可以在AppDelegate的方法didFinishLauchingWithOption中实现相同功能,在那里您可以检查用户是否登录并设置根视图控制器并且不在storyboard中设置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

2
投票

迅速

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
}

1
投票

您无法在启动屏幕中执行此操作,但可以在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
    }

快乐编码希望这有助于:)


1
投票

在AppDelegate中编写此代码

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

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.launch(rootController: ViewController())
        return true
    }

将此方法放在AppDelegate中的任何位置

private func launch(rootController: UIViewController) {
    let frame = UIScreen.main.bounds
    self.window = UIWindow(frame: frame)
    self.window!.rootViewController = rootController
    self.window!.makeKeyAndVisible()
}

0
投票

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

self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("{INSERT_STORYBOARD_ID_HERE}") as? UIViewController
© www.soinside.com 2019 - 2024. All rights reserved.