我正在 Xcode (14.5) 环境中使用 Swift 开发 iOS 应用程序。
主(基础)故事板中有两个名为 ViewController.swift 和 XYZViewController.swift 的不同场景。
首先,ViewController.swift 文件在屏幕上运行。因为这里会出现一个 SpeechScreen 屏幕。然后,重定向到 XYZViewController.swift 文件。 问题从这里开始。是的,会重定向到 XYZViewController.swift。但是,ViewController.swift 文件仍然在后台运行。所以,onboardingLottieView时,动画仍然在后台运行。 重定向到 XYZViewController.swift 文件后,我想关闭 ViewController.swift 场景。但下面的代码无法正常工作。动画仍然在后台运行。
我查看了 iOS 文档
我还查看了此页面。 实际上,这是一个像本页上的问题。这里它关闭第二个视图控制器。我正在尝试关闭第一个视图控制器。
我尝试从导航控制器导航 XYZViewController。但我也无法实现这一目标。
我不是iOS开发专业人士。我正在学习一些新东西。如果您有帮助,我会很高兴。
ViewController.swift 文件如下:
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)
}
}
}
}
我更新了代码如下,问题解决了。也许它对其他人有帮助:
@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
}
}
}
}