Swift3 向以编程方式创建的uibuttons添加操作

Swift3 向以编程方式创建的uibuttons添加操作,swift3,uibutton,xcode8,ios10,Swift3,Uibutton,Xcode8,Ios10,我已经编写了一个类文件,为我的应用程序创建了一个顶部栏,我还在顶部栏添加了按钮,在添加了目标按钮后,添加了按钮,同样不会触发 行menuButton.addTarget(self,action:#选择器(showMenu),for:.touchupine)不会触发函数showMenu 我在主视图文件中创建了一个TN对象(来自类TopNav),并将其添加到视图中,但菜单按钮不会触发点击 import Foundation import UIKit class TopNav{ var t

我已经编写了一个类文件,为我的应用程序创建了一个顶部栏,我还在顶部栏添加了按钮,在添加了目标按钮后,添加了按钮,同样不会触发 行
menuButton.addTarget(self,action:#选择器(showMenu),for:.touchupine)
不会触发函数showMenu

我在主视图文件中创建了一个TN对象(来自类TopNav),并将其添加到视图中,但菜单按钮不会触发点击

import Foundation
import UIKit

class TopNav{

    var topView: UIView = UIView()
    var menuButton: UIButton = UIButton()

    @objc func showMenu(sender: UIButton!) {
        print("show Menu")
    }

    func position(){

        let bounds = UIScreen.main.bounds
        let width = bounds.size.width
        topView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: width, height: 60.0))
        topView.backgroundColor = UIColor.init(colorLiteralRed: 66/255, green: 74/255, blue: 87/255, alpha: 1.0)
        menuButton = UIButton(frame: CGRect(x: (width-40), y: 20.0, width: 30.0, height: 30.0))
        menuButton.setBackgroundImage( UIImage(named:"menu"), for: .normal)
        menuButton.setTitle("", for: UIControlState.normal)
        menuButton.addTarget(self, action: #selector(showMenu), for: .touchUpInside)
        topView.addSubview(menuButton)
    }
}
你可以试试这个

class TopNav {
    func position() {
        ...
        menuButton.addTarget(self, action: #selector(TopNav.showMenu), for: .touchUpInside)
        ...
    }

    func showMenu() {
        //your code
    }
}

更改选择器,如
#选择器(self.showMenu(sender:))
这没有帮助,仍然没有错误,但菜单按钮不会触发