【问题标题】:Swift: When changing ViewControllers: the previous one doesn't dismissSwift:更改 ViewControllers 时:前一个不会关闭
【发布时间】:2017-02-05 08:57:44
【问题描述】:

当谈到 UIKit 和 ViewControllers 时,我是个菜鸟。我正在尝试从SplashScreenViewController 切换到GameViewController,这是一个SKView。 GameViewController 加载正常,因为我可以听到游戏音乐开始,但 SplashScreenController 永远不会从屏幕上消失。所以基本上我有SplashScreenController 留在屏幕上,我可以听到GameViewController 在后台启动。我做错了什么?

这是SplashScreenViewController的代码:

class SplashScreenViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor(red:  46/255, green: 83/255, blue: 160/255, alpha: 1.0)
        let image : UIImage = UIImage(named:"splashScreen.png")!
        let bgImage = UIImageView(image: image)

        bgImage.frame = CGRect(origin: CGPoint(x: 3,y: -3), size: CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height))
        self.view.addSubview(bgImage)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "GameViewController")
        self.present(controller, animated: true, completion: nil)
        self.dismiss(animated: true, completion: nil)
    }

    deinit {
        print("Object with name SplashScreenViewController is being released")
    }
}

【问题讨论】:

  • 很简单,请在dismissViewController方法完成时呈现nextviewcontroller。
  • 有趣的想法,但我想它只有在SplashScreenViewController 被解雇后还剩下一个视图控制器时才有效。

标签: ios swift uiviewcontroller sprite-kit uikit


【解决方案1】:

您可以设置窗口的rootviewcontroller 以将启动画面替换为游戏画面。 代码将如下所示

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window!.rootViewController = gameViewController

【讨论】:

    【解决方案2】:

    据我所知,您想将SplashScreenViewController 替换为您的GameViewController。您当前的代码正在呈现GameViewControllermodally,然后直接将其关闭。所以没有显示新的视图控制器。

    您的代码中的另一个问题是缺少时间间隔,当前初始屏幕已初始化,然后如果您的代码正常工作,它将立即被GameViewController 替换,因此您只会看到GameViewController

    class SplashScreenViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.view.backgroundColor = UIColor(red:  46/255, green: 83/255, blue: 160/255, alpha: 1.0)
            let image : UIImage = UIImage(named:"splashScreen.png")!
            let bgImage = UIImageView(image: image)
    
            bgImage.frame = CGRect(origin: CGPoint(x: 3,y: -3), size: CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height))
            self.view.addSubview(bgImage)
    
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "GameViewController")            
    
            // Here we create a dispatch queue to do some other code after an amount of time. In this case, one second.
            let dispatchTime = DispatchTime.now() + .seconds(1)
            DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
               self.navigationController?.setViewControllers([controller!], animated: false)
            }
        }
    }
    

    此设置将在一秒钟后将 SplashScreenViewController 替换为 GameViewController。为此,对于视图控制器之间的基本所有导航,您必须将您的第一个视图控制器包装到 UINavigationController 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      相关资源
      最近更新 更多