gpt4 book ai didi

swift - 计时器不会调用选择器函数

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:36 25 4
gpt4 key购买 nike

我不确定为什么每次运行我的应用程序时,我的计时器都不会调用更新函数。我已经尝试了一切:使用#selector,在更新结束时添加一个冒号,但没有任何效果。我在我的更新中添加了一个打印语句以确保它到达那里,并且打印语句永远不会打印!我不确定为什么它没有被调用....帮助!附言我知道 selected 确实是 if 语句中的选项之一,我有一个打印语句,它正在工作。

 if (selected == "05 seconds") {
count = 5
timer = Timer(timeInterval: 1.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)

}
else if (selected == "10 seconds") {
count = 10
timer = Timer(timeInterval: 1.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
else if (selected == "20 seconds") {
count = 20
timer = Timer(timeInterval: 1.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
else if (selected == "30 seconds") {
print("I'm here!")
count = 30
timer = Timer(timeInterval: 1.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
else if (selected == "Single Shot") {
SendButton.isHidden = false
countDownLabel.isHidden = true
}
}
func update() {
print("Now I'm in update!")
count = count - 1;
countDownLabel.text = String(count)
}

最佳答案

计时器不起作用,因为您使用的方法要求将计时器显式添加到运行循环中。

使用将计时器隐式添加到运行循环的 API。

 timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(update), userInfo: nil, repeats: true)

PS:#selector 语法更可取,因为它会在编译时检查操作方法是否存在。

关于swift - 计时器不会调用选择器函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40694974/

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