Setting off a function in another class

你离开我真会死。 提交于 2020-01-06 15:28:14

问题


I'm trying to set off a function in my custom tableViewCell-class by using a function in my view controller. I'm really clueless on how to do this, so I tried writing it like this:

TableViewCell.timerStarted()

The function I'm trying to set off looks like this:

func timerStarted(){
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)
}

Where TableViewCell is the name of my class and timerStarted the name of the function. This gives me an error saying it misses an argument inside the parenthesis.

I'm quite stuck here, any suggestions would be appreciated.


回答1:


When you define the function timerStarted inside your TableViewCell class, it is an Instance Method. It's should be called on instances of the TableViewCell. To call the function on the class itself, it should have been defined as a type method. To do that, change your definition of timerStarted by adding class before it

class func timerStarted(){ ... }


来源:https://stackoverflow.com/questions/27943566/setting-off-a-function-in-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!