Swift nstimer 访问其他类的函数

标签 swift class nstimer

在不同类中作为 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/

相关文章:

swift - 中间有scnTube的scnBox

javascript - classed() 和 attr ("class"之间的区别)

ios - 类型 'GameScene has no member ' 延迟'

cocoa - 是否可以在 NSTimer 的间隔内知道当前时间?

ios - Swift 中开关盒的穷举条件

ios - Realm Typecast 问题 Swift

ios - 在临时分发中加载自定义框架时出错

c++ - 如何在类初始化时将类插入到 STL 映射中

C++ 数据结构编程 : Passing Values by Reference?

iphone - 如何使用ntimer显示秒?