在不同类中作为 NStimer 选择器运行该函数时遇到问题。
class VariClass
{
func updateUILoop()
{
print("updating UI")
}
}
在应用程序委托(delegate)中我有
let values = VariClass()
在我的应用程序委托(delegate)中。
let timer = NSTimer(fireDate: NSDate(), interval: 5, target: self, selector: #selector(values.updateUILoop), userInfo: nil, repeats: true)
如果我手动运行该函数,该函数可以完美运行。
getvalues.updateUILoop()
最佳答案
您想要NSTimer
调用 func updateUILoop
关于values
对象,所以定时器回调的目标应该是 values
,不是self
:
let timer = NSTimer(fireDate: NSDate(), interval: 5, target: self.values, selector: #selector(VariClass.updateUILoop), userInfo: nil, repeats: true)
关于Swift nstimer 访问其他类的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38110867/