gpt4 book ai didi

ios - swift 在其他 View Controller 中启动计时器

转载 作者:行者123 更新时间:2023-11-28 16:07:47 27 4
gpt4 key购买 nike

我的计时器功能有问题。我在点击一个 btn (startBtn) 时启动了一个计时器,当我点击另一个 btn (restBtn) 时我将一个新的 View Controller 显示为弹出窗口现在它正确地启动了计时器并且打开新的 View Controller 作为弹出窗口,时间倒计时。

问题是在新的 viewcontroller 中它命中 0 并自行解雇。它应该在第一个 View Controller 中自动启动计时器。

这是我在 mainVC 上的代码:

var timeLeft = 0
var myTimer: Timer!


@IBAction func startBtnPressed(_ sender: AnyObject) {
timeLeft = 0
myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(workoutStartVC.timerRunning), userInfo: nil, repeats: true)
}


@IBAction func restBtnPressed(_ sender: AnyObject) {
print("rest mode button is pressed and i am showing a overlay right now with data count down")
myTimer.invalidate()

// show popup when user interacts with restbtn
let popOpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpVC
self.addChildViewController(popOpVC)
popOpVC.view.frame = self.view.frame
popOpVC.view.backgroundColor = BLUE_COLOR.withAlphaComponent(0.75)
self.view.addSubview(popOpVC.view)
popOpVC.didMove(toParentViewController: self)

timeLbl.text = ""
}

func timerRunning() {
timeLeft = timeLeft + 1
print("the timeLeft is: \(timeLeft)")

**timeLbl.text = "\(timeLeft)"** (HERE I AM GETTING ON THE SECOND TIME WHEN DISMISSING THE POPUPVC A FOUND NILL)

if timeLeft == 30 {
myTimer.invalidate()
timeLbl.text = ""
}
}

这是我在 PopUpVC 上的代码:

var timeLeft = 0
var myTimer: Timer!

override func viewDidLoad() {
super.viewDidLoad()

timeLeft = 10
myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(PopUpVC.timerRunning), userInfo: nil, repeats: true)
}


func timerRunning() {
timeLeft = timeLeft - 1
restModeTimer.text = "\(timeLeft)"

if timeLeft == 0 {
myTimer.invalidate()
restModeTimer.text = ""
self.view.removeFromSuperview()

let worskoutStartVC = workoutStartVC()
worskoutStartVC.myTimer = nil
worskoutStartVC.timeLeft = 0

worskoutStartVC.myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: worskoutStartVC, selector: #selector(worskoutStartVC.timerRunning), userInfo: nil, repeats: true)
}
}

谁能解释为什么在我在下面的代码中加粗的部分展开选项值时发现 nil ?

非常感谢!

最佳答案

据我所见,您似乎正在初始化一个新的 View Controller ...在以下行中:

let worskoutStartVC = workoutStartVC()

因此,当它被创建时,标签的导出连接方式与从 Storyboard初始化 View 时的连接方式不同。

所以我看到您可能想尝试的两件事:

  1. (可能是最佳选择)将对原始 View Controller 的引用传递给第二个 View Controller ,这样您就不必再次创建它。然后您使用该引用来重新创建计时器。因为您现在所做的实际上是每次 PopUpVC 的计时器结束时都创建一个新的 View Controller 。 更多详情在文末

  2. (这会导致初始 VC 的多个副本,因此更多的是帮助理解正在发生的事情)您可以像在上面的代码中所做的那样使用 UIStoryboard() 创建 View Controller 的另一个副本,并且应该正确连接 socket 。

正如您的代码所说:

// show popup when user interacts with restbtn
let popOpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpVC
self.addChildViewController(popOpVC)

同样,选项 1 是 IMO 的最佳选择,尽管可能还有其他模式需要考虑。标签为零的原因是,当您使用 workoutStartVC() 创建 View 时,它没有连接到标签对象。

编辑:添加有关如何传递引用的信息:

PopUpVC 代码可能会更改为引用原始 VC:

var timeLeft = 0
var myTimer: Timer!
var parentVC: workoutStartVC?

override func viewDidLoad() {
super.viewDidLoad()

timeLeft = 10
myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(PopUpVC.timerRunning), userInfo: nil, repeats: true)
}


func timerRunning() {
timeLeft = timeLeft - 1
restModeTimer.text = "\(timeLeft)"

if timeLeft == 0 {
myTimer.invalidate()
restModeTimer.text = ""
self.view.removeFromSuperview()

if let worskoutStartVC = workoutStartVC {
worskoutStartVC.myTimer = nil
worskoutStartVC.timeLeft = 0

worskoutStartVC.myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: worskoutStartVC, selector: #selector(worskoutStartVC.timerRunning), userInfo: nil, repeats: true)
}
}
}

当你转换到这个 View 时,你必须设置这个属性:(片段)

        // show popup when user interacts with restbtn
let popOpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpVC
self.addChildViewController(popOpVC)
popOpVC.parentVC = self
popOpVC.view.frame = self.view.frame
popOpVC.view.backgroundColor = BLUE_COLOR.withAlphaComponent(0.75)
self.view.addSubview(popOpVC.view)
popOpVC.didMove(toParentViewController: self)

注意:已尽力将类名调整为我认为它们来自您的代码的名称,但有些内容可能需要调整。

关于ios - swift 在其他 View Controller 中启动计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39994811/

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