【发布时间】:2017-03-24 02:40:51
【问题描述】:
我有一个这样的嵌套函数,当用户点击我的按钮时我想调用childFunc,但它不起作用
class MyClass {
func parentFunc() {
button.addTarget(self, action: #selector(parentFunc().childFunc), for: .touchUpInside)
func childFunc() {
print("User tapped")
}
}
}
它会引发这样的错误:
Value of tuple type '()' has no member 'childFunc'
有什么方法可以用#selector 执行childFunc 吗?
编辑 1: 我有使用这样的闭包,但我认为这不是一个好方法,因为我必须创建另一个函数
class MyClass {
myClosure: () -> () = {}
func parentFunc() {
button.addTarget(self, action: #selector(runChildFunc), for: .touchUpInside)
func childFunc() {
print("User tapped")
}
myClosesure = childFunc
}
func runChildFunc() {
myClosure()
}
}
【问题讨论】: