2016-11-15 28 views

回答

0
  1. 每一個數字創建於廈門國際銀行按鈕/分鏡

  2. 套裝tag財產(右視圖部分) enter image description here

  3. 任何按鈕按住Ctrl鍵上您的課

  4. 在彈出框中選擇IBAction並輸入名稱 IBAction

  5. 實現該方法:

@IBAction func digitPressed(button: UIButton) { 
    print("digit pressed: \(button.tag)") 
} 

P.S.我不建議對所有按鈕使用相同的方法,只適用於那些與數字非常接近的按鈕。如果你也有+ - * /按鈕,你應該爲每個按鈕添加其他IBActions。

1

或以編程爲任意數量的按鈕(注意:這是雨燕3.0)

func setUp() { 
    var button1, button2: UIButton! 

    let buttons = [button1, button2] 

    for i in 0..<buttons.count { 
     buttons[i]?.tag = i 
    } 

    buttons.forEach({ $0.addTarget(self, action: #selector(MyClass.myAction(_:)), for: .touchUpInside) }) 
} 

func myAction(_ sender: UIButton) { 
    print(sender.tag) 
} 
相關問題