3
我寫了一個類文件來爲我的應用創建一個topBar,我還在按鈕添加到頂部欄後添加了按鈕添加的按鈕,同樣沒有觸發 menuButton.addTarget(self, action: #selector(showMenu), for: .touchUpInside)
不觸發主視圖文件的功能showMenu添加動作以編程方式創建的uibuttons
我創建了一個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)
}
}
改變你的選擇,比如'#selector(self.showMenu(發件人:))' –
這沒有幫助,仍然沒有錯誤,但菜單按鈕犯規觸發 –