【问题标题】:AppDelegate problem when i close Onboarding and run application for second time当我关闭 Onboarding 并第二次运行应用程序时出现 AppDelegate 问题
【发布时间】:2019-12-18 10:10:56
【问题描述】:

我使用 UIPageViewController 启动它的工作,但是当我第二次运行该应用程序时,Xib 不工作

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

        //////OnBoarding
        let lunchbefor = UserDefaults.standard.bool(forKey: "haslunched")
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let lunchstoryboard = UIStoryboard(name: "onboarding", bundle: nil)
        let mainstoryboard = UIStoryboard(name: "Main", bundle: nil)

        var vc: UIViewController
        if lunchbefor{
            window?.rootViewController = MainVC()
            vc = mainstoryboard.instantiateInitialViewController()!

        }else{
            vc = lunchstoryboard.instantiateViewController(withIdentifier: "start")
        }
        UserDefaults.standard.set(true, forKey: "haslunched")
        if lunchbefor == false{

            self.window?.rootViewController = vc
            self.window?.makeKeyAndVisible()
        }else{
            window?.makeKeyAndVisible()
            window?.rootViewController = MainVC()

        }

        return true
    }

错误:

线程 1:致命错误:在展开可选值时意外发现 nil"

在这一行:

vc = mainstoryboard.instantiateInitialViewController()!

【问题讨论】:

  • 不工作是什么意思?
  • 崩溃发生@sazzad-hissain-khan
  • 能否提供出现的错误信息?
  • “线程 1:致命错误:在展开可选值时意外发现 nil”:vc = mainstoryboard.instantiateInitialViewController()!

标签: swift appdelegate uipagecontrol


【解决方案1】:

我找到了答案 我这样做了:

  var vc: UIViewController

    if lunchbefor{
        window?.rootViewController = MainVC()
       // vc = mainstoryboard.instantiateInitialViewController()!
        vc =  MainVC()

    }else{
        vc = lunchstoryboard.instantiateViewController(withIdentifier: "start")
    }

【讨论】:

    【解决方案2】:

    如果您想使用instantiateInitialViewController,您需要在情节提要中选择Is Initial View Controller(屏幕上的红色箭头),您会看到一个指示视图控制器是初始控制器(绿色箭头)

    此外,最好不要使用强制解包 (!),因为当变量为 nil 时,它会使您的应用程序崩溃。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-01
      • 2023-03-09
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      相关资源
      最近更新 更多