我目前正在为我正在处理的应用程序制作自定义的UIView类,并且它要求您传递一个将在UIView的。我需要做的就是将函数传递给init()
函数,并将其存储起来,以便稍后调用它。我目前正在使用的代码如下所示:将函数传递给变量以便稍后可以调用
class CustomUIView: UIView {
private var _tapListener : Void;
init (tapListener:() -> Void){
//Attempt to set _tapListener variable as tap listener does not work :(
_tapListener = tapListener();
//Right now it just executes the function :(
let listener = UITapGestureRecognizer(target: self, action: #selector(callFunction));
self.addGestureRecognizer(listener)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func callFunction() {
//Call the function here
_tapListener();
}
}
如何完成我在此尝试完成的操作?
任何及所有的帮助感激
因为类型你的'private var _tapListener'是错误的。 – matt