Swift 使用嵌套函数作为“UIButton”的操作`

Swift 使用嵌套函数作为“UIButton”的操作`,swift,function,uibutton,selector,target,Swift,Function,Uibutton,Selector,Target,我试图向UIButton添加一个目标,但尝试使用嵌套函数作为其操作时出错 这是我的密码: func createAddView() { let addButton = UIButton() func remove(sender: UIButton) { print("Remove") } addButton.addTarget(self, action: #selector(remove(sender:)), for: .touchUpInsi

我试图向UIButton添加一个目标,但尝试使用嵌套函数作为其操作时出错

这是我的密码:

func createAddView() {
    let addButton = UIButton()

    func remove(sender: UIButton) {
        print("Remove")
    }

    addButton.addTarget(self, action: #selector(remove(sender:)), for: .touchUpInside)
}
它给了我这样的警告:

warning: No method declared with Objective-C selector 'remove'.
我需要将“remove”函数嵌套在“createAddView”函数中,因为我需要删除并淡出正在“createAddView”函数中创建的一些其他UIView


有人知道我怎么做吗?

因为func remove是在createAddView()方法中创建的

以下是固定代码:

func createAddView() {
    let view = UIView() //added for testing purposes 
    view.frame = CGRect(x: 0, y: 0, width: 320  , height: 640) //added for testing purposes
    let addButton = UIButton()
    addButton.frame = CGRect(x: 0, y: 0, width: 100, height: 50) //added for testing purposes


    view.addSubview(addButton)



    addButton.addTarget(self, action: #selector(remove(sender:)), for: .touchUpInside)

}

func remove(sender: UIButton) {
    UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.allowUserInteraction, animations: { () -> Void in
        print("Add")
    }) {(Bool) -> Void in
        print("Done")
    }
}

无法执行此操作,因为
func remove
仅存在于
func createAddView
块中。
将一个
#选择器()
添加到多个
UIControl
没有限制。因此,您可以在类块中声明
func remove
,并在每次创建新按钮时将其添加为
#selector

您不能将嵌套函数用作目标:将
func remove(发送方:UIButton)
移动到
createAddView()
之外,然后重试?