我已经编写了一个类文件来为我的应用程序创建一个topBar,我还在向添加的按钮添加目标后添加按钮到顶部栏,同样不会触发
第menuButton.addTarget(self, action: #selector(showMenu), for: .touchUpInside)
行不会触发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)
}
}
答案 0 :(得分:2)
你可以试试这个。
class TopNav {
func position() {
...
menuButton.addTarget(self, action: #selector(TopNav.showMenu), for: .touchUpInside)
...
}
func showMenu() {
//your code
}
}