Swift 在AppDelegate中执行segue
当Firebase令牌无效时,我希望将用户重定向到我的应用程序loginVC。这是我的代码:Swift 在AppDelegate中执行segue,swift,segue,appdelegate,Swift,Segue,Appdelegate,当Firebase令牌无效时,我希望将用户重定向到我的应用程序loginVC。这是我的代码: func userErrorLogout() { print("user error logout") let storyboard = UIStoryboard(name: "Main", bundle: nil); let viewController: ViewController = storyboard.instantiateViewController(withI
func userErrorLogout() {
print("user error logout")
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: ViewController = storyboard.instantiateViewController(withIdentifier: "loginVC") as! ViewController;
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);
}
我在“let rootViewController:…=2019-11-27 13:03:49.364864+0100 Geo[25133:2193886]上收到一个错误致命错误:在展开可选值:file时意外发现nil
由于SWIFT对我来说是新手,我无法调试此消息。如何执行重定向?您需要为您的rootViewController分配值。请尝试以下操作:
func userErrorLogout() {
print("user error logout")
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: ViewController = storyboard.instantiateViewController(withIdentifier: "loginVC") as! ViewController;
let nav = UINavigationController(rootViewController: viewController)
self.window?.rootViewController = nav
}您需要使用以下代码:
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: ViewController = storyboard.instantiateViewController(withIdentifier: "loginVC") as! ViewController;
let window = (UIApplication.shared.delegate as! AppDelegate).window!
DispatchQueue.main.async {
window.rootViewController = viewController
}
你需要将viewcontroller设置为window。你可以像下面这样做。这段代码还有过渡动画,它提供淡入过渡的效果
// MARK: WINDOW SET
func gotoIntro() {
UIView.transition(with: window!, duration: 0.5, options: .transitionCrossDissolve, animations: {
UIView.performWithoutAnimation({
let introStoryboard = UIStoryboard(name: "Intro", bundle: nil)
let navigationController = introStoryboard.instantiateInitialViewController() as! UINavigationController
self.window?.rootViewController = navigationController
})
}, completion: nil)
}
从iOS 13开始,AppDelegate不再能够在启动开始时推送VC(不是100%确定) 如果希望从根目录显示视图,则必须使用SceneDelegate。 要做到这一点,只需在ScenedLegate文件中执行即可。我已经在自己的应用程序上测试了这种方法,并且100%有效
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
// check if the token is valid
if valid {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "yourVC")
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
}
else {
// token is invalid
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(identifier: "invalidVC")
let rootNC = UINavigationController(rootViewController: rootVC)
self.window?.rootViewController = rootNC
self.window?.makeKeyAndVisible()
}
}
确保您的
self.window!.rootViewController
是UINavigationController
。进入情节提要并将初始ViewController添加为UINavigationController