【问题标题】:Array of UIButtons in Swift 4Swift 4 中的 UIButton 数组
【发布时间】:2018-10-08 15:41:54
【问题描述】:

我在 UiKit 中使用 UIButtons 制作了一系列复选框:

@IBOutlet weak var Box1: UIButton!
@IBOutlet weak var Box2: UIButton!
@IBOutlet weak var Box3: UIButton!
....
@IBOutlet weak var Box59: UIButton!




// Gives the button an action
@IBAction func Box1(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
}
@IBAction func Box2(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
}
@IBAction func Box3(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
}
    ....
@IBAction func Box59(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
}




// Creates button images of checkbox and unchecked box
var BoxON = UIImage(named: "CheckBox")
var BoxOFF = UIImage(named:"UnCheckBox")




// Allows the button to be set to the image, if selected or not 
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    Box1.setImage(BoxOFF, for: .normal)
    Box1.setImage(BoxON, for: .selected)

    Box2.setImage(BoxOFF, for: .normal)
    Box2.setImage(BoxON, for: .selected)

    Box3.setImage(BoxOFF, for: .normal)
    Box3.setImage(BoxON, for: .selected)
    ....
    Box59.setImage(BoxOFF, for: .normal)
    Box59.setImage(BoxON, for: .selected)

    }

这是制作尽可能多的复选框所需的所有代码。但是,每个复选框都需要创建/移动一个按钮到情节提要上的正确位置,将情节提要中的按钮链接到刚刚编码的按钮变量。这可能需要很长时间,因为每个视图控制器需要超过 100 个按钮。

有没有更快的方法通过制作一组按钮来做到这一点?或类似的东西?

【问题讨论】:

  • 您可以制作表格视图或集合视图,然后将单元格设置为您想要的按钮样式吗?
  • 如果你真的需要在一个数组中有很多按钮,你可以在代码中循环创建这些按钮。我还建议使用 UITableView 或 UICollectionView
  • 这是个好主意。集合视图中的每个单元格可以移动到视图中的随机位置吗?单元格也可以像按钮一样操作吗?

标签: ios arrays swift button


【解决方案1】:

可以将IBOutlet 声明为数组

  • 创建一个IBOutlet 作为数组

    @IBOutlet var boxes : [UIButton]!
    
  • 将所有按钮连接到同一个插座(按所需顺序)

  • viewDidAppear 中使用循环或forEach

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        boxes.forEach {
            $0.setImage(BoxOFF, for: .normal)
            $0.setImage(BoxON, for: .selected)
        }
    }
    
  • 为按钮添加唯一标签(可选)。

  • 创建一个IBAction 并将所有按钮连接到操作
  • 使用switch语句通过数组中的索引或标签来区分按钮

    @IBAction func boxTouched(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        let index = boxes.index(of: sender)!
        switch index {
           // handle the cases
        }
    }
    
    @IBAction func boxTouched(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        switch sender.tag {
           // handle the cases
        }
    }
    

【讨论】:

  • Plus1.凉爽的。不知道我们可以将出口声明为数组!
  • @BadhanGanesh 它被称为IBOutletCollection。当您选择多个项目(相同类型)并从 IB 画布控制拖动到助手编辑器中的目标类时,您会获得该选项
  • 是的,我在连接常规插座时见过它们。但想知道那是什么。现在我可以平静地死去。 ?
  • @BadhanGanesh 长命百岁 ?
  • 太棒了!如何为按钮添加标签?或者我将如何索引它们?
【解决方案2】:

我已经创建了一系列复选框(按钮)以及文本(标签)。您可以选择和取消选择任何按钮。

func addCheckBoxButtonsForSecondContainerView ()
{
    var xPosition = 0
    var yPosition = 0
    for i in 0..<likedArray.count
           {
               let secondViewCheckBox = UIButton(frame: CGRect(x: xPosition, y: yPosition, width: 18, height: 18))
               secondViewCheckBox.layer.borderWidth = 1
               secondViewCheckBox.layer.borderColor = UIColor(red: 1.0 / 255.0, green: 202.0 / 255.0, blue: 91.0 / 255.0, alpha: 1.0).cgColor
               secondViewCheckBox.addTarget(self, action: #selector(secondViewButtonAction(_:)), for: .touchUpInside)
               secondViewCheckBox.tag = i
               secondViewCheckBox.layer.cornerRadius = 2
               self.secondContainerView.addSubview(secondViewCheckBox)//adding button to container view
            //In the below line I'm creating label for title.
            let labelTitle = UILabel(frame: CGRect(x: xPosition+25, y: yPosition, width: 130, height: 18))  //UIButton(frame: CGRect(x: xPosition+30, y: yPosition, width: 100, height: 18))
            labelTitle.text = "  " + self.likedArray[i]
            labelTitle.font = UIFont.RummyLatoRegular(size: 12)
            labelTitle.tag = secondViewCheckBox.tag
            labelTitle.textAlignment = .left
            labelTitle.isUserInteractionEnabled = true
            labelTitle.textColor = UIColor.white
            labelTitle.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(secondViewLabelAction(_:))))
            self.secondContainerView.addSubview(labelTitle)//adding label to the containerview

            if i != 0 && (i+1)%3 == 0
            {
                xPosition = 0
                yPosition = Int(secondViewCheckBox.frame.origin.y+secondViewCheckBox.frame.size.height+15)
            }
            else
            {
                xPosition += 160
            }
           }
    
}

//接下来我们在这里看到按钮(复选框)的操作

 @objc func secondViewButtonAction(_ sender: UIButton)
   {
    if arrayofSelectedIndexes.contains(sender.tag)
    {
        for i in 0..<arrayofSelectedIndexes.count
        {
            if arrayofSelectedIndexes[i] == sender.tag
            {
                arrayofSelectedIndexes.remove(at: i)
                sender.setBackgroundImage(UIImage.init(named: ""), for: .normal)
                break
            }

        }
    }
    else
    {
        arrayofSelectedIndexes.append(sender.tag)
        sender.setBackgroundImage(UIImage.init(named: "npsTickIcon"), for: .normal)
    }

}

//这是标题标签动作

 @objc func secondViewLabelAction(_ sender: UITapGestureRecognizer)
      {
        for subview in self.secondContainerView.subviews {
            if let button = subview as? UIButton {
                let searchlbl:UILabel = (sender.view as! UILabel) //just to get sender tag
                if searchlbl.tag == subview.tag
                {
                    if arrayofSelectedIndexes.contains(searchlbl.tag)
                    {
                        for i in 0..<arrayofSelectedIndexes.count
                        {
                            if arrayofSelectedIndexes[i] == searchlbl.tag
                            {
                                arrayofSelectedIndexes.remove(at: i)
                                button.setBackgroundImage(UIImage.init(named: ""), for: .normal)
                                break
                            }
                        }
                    }
                    else{
                        arrayofSelectedIndexes.append(searchlbl.tag)
                    button.setBackgroundImage(UIImage.init(named: "npsTickIcon"), for: .normal)
                    }
                }
                
            }
        }

如果您需要更多说明,请告诉我。用户可以点击按钮或标题标签。将执行相同的操作。我在此处附上屏幕截图以供输出。 [1]:https://i.stack.imgur.com/cXoww.png

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多