Reputation: 61
I'm a novice at Swift programming.
I've created an app with multiple identical buttons. I created one button with an action and then copied this button multiple times so that it uses the same action.
Is there an easy way to add all these buttons (there are around 100) to an array, so that I can change for example the button image for all of them in one go?
Since there are so many buttons, I'd like to avoid having to add them all, one-by-one, to an Outlet collection.
As mentioned I'm a novice, so the simpler the better.
Edit All the instances of the button has the tag '1' if that's any help and was created via the main storyboard.
Thanks in advance, Thomas
Upvotes: 2
Views: 6016
Reputation: 154691
Updated Answer:
Your buttons are contained within UIStackView
s. There is one verticalStackView
that contains multiple horizontalStackView
s (each with one row of UIButton
s).
Create an @IBOutlet
to the verticalStackView
, and then add your buttons to the class property called allButtons
with the following code in viewDidLoad
:
class ViewController: UIViewController {
@IBOutlet weak var verticalStackView: UIStackView!
var allButtons = [UIButton]()
override func viewDidLoad() {
super.viewDidLoad()
for case let horizontalStackView as UIStackView in verticalStackView.arrangedSubviews {
for case let button as UIButton in horizontalStackView.arrangedSubviews {
allButtons.append(button)
}
}
}
}
Original Answer:
To create an array of all UIButton
s with tag == 1
:
Use conditional cast as? UIButton
along with compactMap
to find all of the buttons in outerView.subviews
and then filter
to select those buttons with tag == 1
:
class ViewController: UIViewController {
@IBOutlet weak var outerView: UIView!
var allButtons: [UIButton] = []
override func viewDidLoad() {
super.viewDidLoad()
allButtons = outerView.subviews
.compactMap { $0 as? UIButton }
.filter { $0.tag == 1 }
}
}
Note: outerView
is the UIView
that contains the buttons. Create an @IBOutlet
to that view to make this code work.
Upvotes: 2
Reputation: 428
1st step: Connect all button to single IBAction in swift file
2nd step : Give all button a common special tag like 1010, 1007,.. etc
3rd step: write code given below in your IBAction
@IBAction func didPressBtn(_ sender: Any) {
for btn in self.view.subviews{
if btn.tag == 1010
{
let btnRefrence = btn as! UIButton
btnRefrence.setImage(img, for:.normal)
}
}
}
Hope this help
Upvotes: 0
Reputation: 90
let arrayOfButtons: [UIButton]
for i in 0....100 {
let button = UIButton()
button.setImage(UIImage, forState: .normal)
button.setTitle("test", for: .normal)
button.addTarget(self, action: #selector(clickBackButton(_:)), for: .touchUpInside)
arrayOfButtons.append(button)
}
@IBAction func clickBackButton(_ sender: UIButton)
{
}
You can do with any count, i have uesd to add 100 button with title and image.
Upvotes: 1
Reputation: 9
1- Make an array of buttons
let arrayOfButtons: [UIButton]
2- initialize it (give it a value) 3- then use loops to change values such as setting a button image
for button in arrayOfButtons {
button.setImage(UIImage, forState: UIControlState.Normal)
}
Upvotes: 1