gpt4 book ai didi

ios - 如何弹出初始 View Controller

转载 作者:行者123 更新时间:2023-11-30 12:11:21 24 4
gpt4 key购买 nike

假设我有两个 View Controller , View Controller A 和 View Controller B

class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// it runs this function every 5 seconds
Timer.scheduledTimer(5, target: self,selector: #selector(ViewControllerA.printNumber), userInfo: nil, repeats: true)
}

@IBAction func callViewControllerBButtonClicked(_ sender: UIButton) {
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerBID") as? ViewControllerB {

self.present(vc, animated: true, completion: nil)
}

}

func printNumber() {
print(0)
}
}

每当有人点击callViewControllerBButtonClicked()按钮时,它就会实例化一个新的 View Controller ,即ViewControllerB,并将其呈现在ViewControllerA之上。我现在面临的问题是,即使我已经在 ViewControllerB 上,它仍然运行这个函数

Timer.scheduledTimer(5, target: self,selector: #selector(ViewControllerA.printNumber), userInfo: nil, repeats: true)

如何弹出 ViewControllerA?

最佳答案

试试这个代码 -

class ViewControllerA: UIViewController {
var timer : Timer!
override func viewDidLoad() {
super.viewDidLoad()
// it runs this function every 5 seconds
timer = Timer.scheduledTimer(5, target: self,selector: #selector(ViewControllerA.printNumber), userInfo: nil, repeats: true)
}

@IBAction func callViewControllerBButtonClicked(_ sender: UIButton) {
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerBID") as? ViewControllerB {
timer.invalidate()
self.present(vc, animated: true, completion: nil)
}

}

func printNumber() {
print(0)
}
}

关于ios - 如何弹出初始 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46014260/

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