【问题标题】:Assign tags for dynamic UIButtons为动态 UIButton 分配标签
【发布时间】:2018-02-23 10:39:08
【问题描述】:

我在http://helpmecodeswift.com/advanced-functions/generating-uibuttons-loop找到了UIButtons 的动态视图示例

我将此代码插入到我的应用程序中,但遗憾的是我找不到为每个按钮设置标签的方法。
我的目标是知道点击了哪个按钮,计算每个按钮的点击量并将此信息保存到我的数据库中。

override func viewDidLoad() {
    super.viewDidLoad()

    var arrayOfVillains = ["santa", "bugs", "superman", "batman"]

    var buttonY: CGFloat = 20
    for villain in arrayOfVillains {

        let villainButton = UIButton(frame: CGRect(x: 50, y: buttonY, width: 250, height: 30))
        buttonY = buttonY + 50  // we are going to space these UIButtons 50px apart

        villainButton.layer.cornerRadius = 10  // get some fancy pantsy rounding
        villainButton.backgroundColor = UIColor.darkGrayColor()
        villainButton.setTitle("Button for Villain: \(villain)", forState: UIControlState.Normal) // We are going to use the item name as the Button Title here.
        villainButton.titleLabel?.text = "\(villain)"
        villainButton.addTarget(self, action: "villainButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(villainButton)  // myView in this case is the view you want these buttons added
    }
}

func villainButtonPressed(sender:UIButton!) {

    if sender.titleLabel?.text != nil {
        println("You have chosen Villain: \(sender.titleLabel?.text)")
    } else {

        println("Nowhere to go :/")

    }

}

}

那么如何在代码中为/从每个按钮设置和获取标签(在这个例子中)? 提前谢谢!

【问题讨论】:

    标签: ios swift uibutton tags


    【解决方案1】:

    您可以使用枚举数组在迭代中访问当前项目的索引并将其用作标记:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        var arrayOfVillains = ["santa", "bugs", "superman", "batman"]
    
        var buttonY: CGFloat = 20  
        for (index, villain) in arrayOfVillains.enumerated() {
    
            let villainButton = UIButton(frame: CGRect(x: 50, y: buttonY, width: 250, height: 30))
            buttonY = buttonY + 50  // we are going to space these UIButtons 50px apart
    
            // set the tag to current index
            villainButton.tag = index
    
            villainButton.layer.cornerRadius = 10  // get some fancy pantsy rounding
            villainButton.backgroundColor = UIColor.darkGrayColor()
            villainButton.setTitle("Button for Villain: \(villain)", forState: UIControlState.Normal) // We are going to use the item name as the Button Title here.
            villainButton.titleLabel?.text = "\(villain)"
            villainButton.addTarget(self, action: "villainButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    
            self.view.addSubview(villainButton)  // myView in this case is the view you want these buttons added
            }
        }       
    }
    
    @objc func villainButtonPressed(sender:UIButton!) {
    
        // tag of pressed button
        print(">>> you pressed button with tag: \(sender.tag)")
    
        if sender.titleLabel?.text != nil {
            println("You have chosen Villain: \(sender.titleLabel?.text)")
        } else {
    
            println("Nowhere to go :/")
    
        }        
    }
    

    【讨论】:

      【解决方案2】:

      您应该为此使用基于整数的 for 循环。

      for i in 0 ..< arrayOfVillains.count {
          let villain = arrayOfVillains[i]
          let villainButton = UIButton(frame: CGRect(x: 50, y: buttonY, width: 250, height: 30))
          villainButton.tag = i
      }
      

      【讨论】:

        【解决方案3】:

        你可以这样做,

        var buttonTag = 1
        for villain in arrayOfVillains {
           let villainButton = UIButton(frame: CGRect(x: 50, y: buttonY, width: 250, height: 30))
           villainButton.tag = buttonTag
           buttonTag += 1
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-09
          相关资源
          最近更新 更多