gpt4 book ai didi

ios - 如何在 Swift 中关闭当前的 ViewController 并更改为新的 ViewController?

转载 作者:行者123 更新时间:2023-11-28 12:07:38 25 4
gpt4 key购买 nike

我是 iOS swift 的新手。我有三个 ViewController。页面 A 是根 Controller ,它将呈现给页面 B。它在页面 B 中有一个计时器。 5秒后,View会从page-B切换到page-C,同时关闭page-B。

ViewControll-B

class AViewController: UIViewController {

var timer: Timer?

override func viewDidLoad() {
super.viewDidLoad()

//set the timer , and chagne view to C ViewController
Timer.scheduledTimer(timeInterval: 5,
target: self,
selector: #selector(self.changeToAnswerView),
userInfo: nil,
repeats: false)
}


@objc func changeToAnswerView() {
dismissLoader()
}

func dismissLoader() {
dismiss(animated: true) {
print("Dismissing Loader view Controller")
}
}

override func viewWillDisappear(_ animated: Bool) {
//change view to Answer ViewController
let filterVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CViewControllerID")
filterVC.modalPresentationStyle = UIModalPresentationStyle.custom
self.present(filterVC, animated: true, completion: nil)
}

}

计时器执行 5 秒后,BViewController 将关闭自身并呈现给 BViewController

但是会出现如下错误:

whose view is not in the window hierarchy

我错过了什么吗?

问题:如何在 Swift 中关闭当前的 ViewController 并更改为新的 ViewController?

提前致谢。

最佳答案

这是您可以尝试的工作代码

您的 Controller 已被解雇并倾向于创建一个新的 Controller

import UIKit

class pdfVC: UIViewController
{

var timer : Timer?

override func viewDidLoad()
{
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(pdfVC.timerAction), userInfo: nil, repeats: false)
}

@objc func timerAction()
{
if timer != nil {
timer?.invalidate()
dismiss(animated: true, completion: {
print("Dismissed")
})
}
}

override func viewWillDisappear(_ animated: Bool) {
if self.isBeingDismissed {
let filterVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "demoViewController")
filterVC.modalPresentationStyle = UIModalPresentationStyle.custom
print("called")
self.presentingViewController?.present(filterVC, animated: true, completion: nil)
}
}
}

输出

enter image description here

关于ios - 如何在 Swift 中关闭当前的 ViewController 并更改为新的 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49107167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com