Swift 4.0 延迟启动屏幕导致项目消失

Swift 4.0 Delayed Launch Screen Causes Items to disappear

我正在使用 Swift 4.0 创建一个应用程序,我正试图延迟启动屏幕。初始屏幕会休眠 2 秒,然后 "a FusionPointInc application" 标签消失,屏幕停留在红色幕布背景图像(第二张图像)。它将留在那里,不会显示第二个屏幕(历史上的剧院舞台屏幕)

这是我的初始屏幕代码:

override func viewDidLoad() {
    super.viewDidLoad()

    sleep(2)

    showNavController()
}

func showNavController() {
    performSegue(withIdentifier: "showMainView", sender: self)
}

初始屏幕的更多代码(将过渡到第二个屏幕):

`class showMainView: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    //performSegue(withIdentifier: "showSegue", sender: self)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

此屏幕将打开,当提示继续时,屏幕将仅显示背景(第二张图片)。

中间屏幕代码在 ViewController.swift 文件中,其中没有实际代码(只有预先格式化的基本代码)

我测试过,当我删除初始屏幕时,应用程序可以正常加载。但是,我希望延迟初始屏幕以便可以看到 "a FusionPointInc application"!

您正在主线程上调用睡眠。

相反,您应该这样做:

override func viewDidLoad() {
    super.viewDidLoad()

    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
       self.showNavController()
    }
}

这将在两秒后毫无问题地调用 showNavController