【问题标题】:Two Button invokes same function两个按钮调用相同的功能
【发布时间】:2014-10-21 10:55:14
【问题描述】:

我需要用两个不同的按钮调用相同的函数。此函数包含对另一个函数的调用,该函数需要一个 Bool 值,但以图形方式将两个按钮连接到代码我无法区分对该函数的两个不同调用。出于这个原因,我创建了两个相同的函数来传递不同的 Bool 值。我希望你的回答。向所有人致以最诚挚的问候。提前致谢

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    我认为您有两个按钮在视图中,并且您希望将这两个按钮连接起来具有相同的功能,因此您可以这样做,首先为第一个按钮创建一个功能。

    之后,您可以像这样将此功能与另一个按钮连接:

    因此,您可以为两个不同的按钮创建相同的功能。

    编辑此答案

    您可以为两个按钮指定不同的名称,例如 Button 1Button 2

    之后你可以这样做:

    class ViewController: UIViewController {
    
    var button1 : Bool = Bool()
    var button2 : Bool = Bool()
    
    @IBAction func ButtonTapped(sender: UIButton) {
        if sender.titleLabel?.text == "Button 1"{
            button1 = true
            println("Button 1 Tapped")
            println(button1)
        }
        if sender.titleLabel?.text == "Button 2"{
            button2 = true
            println("Button 2 tapped")
            println(button2)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-18
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      • 2020-10-25
      相关资源
      最近更新 更多