本文介绍了在另一个类中设置一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在我的视图控制器中使用一个函数在我的自定义tableViewCell类中设置一个函数。我真的很无知如何做到这一点,所以我试着写这样:

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()

我要设置的功能如下:

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

其中TableViewCell是我的类的名称,timerStarted是函数的名称。这给我一个错误,说它错过括号内的参数。

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.

推荐答案

当您在 TableViewCell 类中定义函数 timerStarted 时,它是一个实例方法。它应该在TableViewCell的实例上调用。要调用类本身的函数,它应该被定义为一个类型方法。要这样做,通过在之前添加 class 来更改 timerStarted 的定义

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(){ ... }

这篇关于在另一个类中设置一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 04:17
查看更多