【问题标题】:Perform segue in AppDelegate在 AppDelegate 中执行 segue
【发布时间】:2020-03-22 23:49:45
【问题描述】:

当 Firebase 令牌无效时,我希望用户重定向到我的应用 loginVC。这是我的代码:

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);
}

我收到错误“让 rootViewController:.... = 2019-11-27 13:03:49.364864+0100 Geo[25133:2193886] 致命错误:在展开可选值时意外发现 nil:文件

由于 SWIFT 对我来说是新手,因此我无法调试此消息。如何执行重定向?

【问题讨论】:

标签: swift segue appdelegate


【解决方案1】:

您需要使用以下代码:

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
}

【讨论】:

    【解决方案2】:

    您需要为您的 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
    

    }

    【讨论】:

      【解决方案3】:

      您需要将视图控制器设置为窗口。你可以像下面那样做。这段代码也有过渡动画,它可以淡入淡出过渡。

          // 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)
      }
      

      【讨论】:

        【解决方案4】:

        从 iOS 13 开始,不再可以从 AppDelegate 在启动时推送 VC(不是 100% 确定)。

        如果您希望从根目录呈现视图,则必须使用 SceneDelegate。 为此,只需在 SceneDelegate 文件中执行此操作。我已经在自己的应用程序上测试了这种方法,并且 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()
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-31
          • 1970-01-01
          • 1970-01-01
          • 2019-07-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多