简体   繁体   中英

Swift: When changing ViewControllers: the previous one doesn't dismiss

I'm a noobie when it comes to UIKit and ViewControllers. I'm trying to switch from SplashScreenViewController to GameViewController which is a SKView. GameViewController is loaded ok as I can hear the game music starting, but the SplashScreenController never dismisses from the screen. So basically I have SplashScreenController staying on screen and I can hear the GameViewController starting in the background. What am I doing wrong?

This is the code for the 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")
    }
}

You can set rootviewcontroller of your window to replace splash screen with game screen. The code will look like this

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

As far as I can tell, you want to replace the SplashScreenViewController with your GameViewController . Your current code is presenting the GameViewController modally and directly after that dismissing it. So the new view controller isn't shown.

Another problem in your code is the missing gap in timing, currently the splash screen get's initialized and right after that it would be replaced by the GameViewController if your code was working, so you would just see the 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)
        }
    }
}

This setup will replace the SplashScreenViewController with the GameViewController after one second. For that to work, and for basically all navigation between view controllers, you must wrap your first view controller into a UINavigationController .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM