2016-10-13 69 views
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) 

    } 

} 
+2

改變你的選擇,比如'#selector(self.showMenu(發件人:))' –

+0

這沒有幫助,仍然沒有錯誤,但菜單按鈕犯規觸發 –

回答

2

你可以試試這個。

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

    func showMenu() { 
     //your code 
    } 
} 
相關問題