perps4214213
perps4214213

Reputation: 33

How to dismiss the first scene in UIKit Application?

I am developing an iOS application with Swift in Xcode (14.5) environment.

There are two different scenes named ViewController.swift and XYZViewController.swift in the Main (Base) storyboard.

First of all, the ViewController.swift file runs on the screen. Because a SpeechScreen screen appears here. Then, redirection is made to the XYZViewController.swift file. The problem starts here. Yes, redirection to XYZViewController.swift occurs. However, the ViewController.swift file still runs in the background. So, onboardingLottieView, the animation still runs in the background. After redirecting to the XYZViewController.swift file, I want to close the ViewController.swift scene. But the code below does not work correctly. The animation still works in the background.

I took a look at the iOS documentation

I also reviewed this page. Actually, it is a problem like the one on this page. Here it closes the Second View Controller. I am trying to close the First view controller.

I tried navigate XYZViewController from the navigation controller. But I couldn't achieve this either.

I'm not a professional in iOS development. I'm learning some new things. I would be glad if you help.

ViewController.swift file as follows:

import UIKit
import Lottie
class ViewController: UIViewController {
    @IBOutlet weak var onboardingLottieView: LottieAnimationView!
    //...
    override func viewDidLoad() {
        super.viewDidLoad()
        self.showSpeechScreen()
    }
    
    func showSpeechScreen() {
        // onboardingLottieView...
        onboardingLottieView.play()
        Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(presentNextViewController), userInfo: nil, repeats: false)
    }
    @objc func presentNextViewController() {
        // Dismiss the current view controller
        self.dismiss(animated: true) {
            // Redirect to XYZViewController
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            if let vc = storyboard.instantiateViewController(withIdentifier: "XYZViewController") as? XYZViewController {
                vc.modalPresentationStyle = .automatic
                vc.modalTransitionStyle = .crossDissolve
                self.present(vc, animated: true)
            }
        }
    }
}

Upvotes: 0

Views: 84

Answers (1)

perps4214213
perps4214213

Reputation: 33

I updated the code as below and the problem was solved. Maybe it helps someone else:

@objc func presentNextViewController() {
        // Dismiss the current view controller
        self.dismiss(animated: true) {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            if let vc = storyboard.instantiateViewController(withIdentifier: "XYZViewController") as? XYZViewController {
                if let sceneDelegate = self.view.window?.windowScene?.delegate as? SceneDelegate {
                    sceneDelegate.window?.rootViewController = vc
                }
            }
        }
    }

Upvotes: 0

Related Questions