How to set values to multiple buttons or labels at once?
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
Is it possible to set, for instance, .isHidden
to multiple buttons at once, instead of:
button1.isHidden = true
button2.isHidden = true
button3.isHidden = true
Something like: button1, button2, button3.isHidden = true
.
swift
add a comment |Â
up vote
9
down vote
favorite
Is it possible to set, for instance, .isHidden
to multiple buttons at once, instead of:
button1.isHidden = true
button2.isHidden = true
button3.isHidden = true
Something like: button1, button2, button3.isHidden = true
.
swift
It seems like putting the buttons in an array makes sense.
â LAD
Aug 10 at 20:05
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
Is it possible to set, for instance, .isHidden
to multiple buttons at once, instead of:
button1.isHidden = true
button2.isHidden = true
button3.isHidden = true
Something like: button1, button2, button3.isHidden = true
.
swift
Is it possible to set, for instance, .isHidden
to multiple buttons at once, instead of:
button1.isHidden = true
button2.isHidden = true
button3.isHidden = true
Something like: button1, button2, button3.isHidden = true
.
swift
edited Aug 10 at 20:30
LAD
1,420519
1,420519
asked Aug 10 at 20:00
Anvil
895
895
It seems like putting the buttons in an array makes sense.
â LAD
Aug 10 at 20:05
add a comment |Â
It seems like putting the buttons in an array makes sense.
â LAD
Aug 10 at 20:05
It seems like putting the buttons in an array makes sense.
â LAD
Aug 10 at 20:05
It seems like putting the buttons in an array makes sense.
â LAD
Aug 10 at 20:05
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
accepted
Put them in an array and iterate through the array.
[button1, button2, button3].forEach
$0.isHidden = true
add a comment |Â
up vote
7
down vote
In addition to @ukim's answer, you can use an Outlet Collection.
In you storyboard, drag from your first button and select Outlet Collection rather than Outlet as you would normally do.
Which gives youâ¦
@IBOutlet var buttons: [UIButton]!
Then connect all your other buttons to the same @IBOutlet
You can then say
buttons.forEach
$0.isHidden = true
add a comment |Â
up vote
4
down vote
You can also create Array
extension. It also make more sense to constraint element type to UIButton
, such that you can't call it for any other type of array.
Something like this,
extension Array where Element == UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
Then, using it like so,
[button1, button2, button3].hide() // hide buttons
[button1, button2, button3].show() // show
Extending collection makes more sense in this case, which gives more flexibility as the hide / show could be used with ArraySlices then.
Here is how you would do this,
extension Collection where Element: UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
func toggleVisibility()
forEach $0.isHidden = !$0.isHidden
And with this you can do some cool thing like,
// hide all but not first
myArrayOfButtons.dropFirst().hide()
// hide buttons in indexes 0 to 1
myArrayOfButtons[0 ... 1].hide()
// show all buttons but not last
myArrayOfButtons.dropLast().show()
// hide first 2 buttons
myArrayOfButtons.prefix(2).hide()
// show last button
myArrayOfButtons.suffix(1).show()
// toggle visibility of first 2
myArrayOfButtons.prefix(2).toggleVisibility()
add a comment |Â
up vote
3
down vote
You could also create an IBOutlet collection
@IBOutlet var multiButtons: [UIButton]!
Do do this: when you control-drag from the button to the code, select Outlet collection ; then control drag other buttons to this outlet collection.
Now, you can
for button in multiButtons
button.isHidden = true
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
Put them in an array and iterate through the array.
[button1, button2, button3].forEach
$0.isHidden = true
add a comment |Â
up vote
6
down vote
accepted
Put them in an array and iterate through the array.
[button1, button2, button3].forEach
$0.isHidden = true
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
Put them in an array and iterate through the array.
[button1, button2, button3].forEach
$0.isHidden = true
Put them in an array and iterate through the array.
[button1, button2, button3].forEach
$0.isHidden = true
answered Aug 10 at 20:01
ukim
1,415615
1,415615
add a comment |Â
add a comment |Â
up vote
7
down vote
In addition to @ukim's answer, you can use an Outlet Collection.
In you storyboard, drag from your first button and select Outlet Collection rather than Outlet as you would normally do.
Which gives youâ¦
@IBOutlet var buttons: [UIButton]!
Then connect all your other buttons to the same @IBOutlet
You can then say
buttons.forEach
$0.isHidden = true
add a comment |Â
up vote
7
down vote
In addition to @ukim's answer, you can use an Outlet Collection.
In you storyboard, drag from your first button and select Outlet Collection rather than Outlet as you would normally do.
Which gives youâ¦
@IBOutlet var buttons: [UIButton]!
Then connect all your other buttons to the same @IBOutlet
You can then say
buttons.forEach
$0.isHidden = true
add a comment |Â
up vote
7
down vote
up vote
7
down vote
In addition to @ukim's answer, you can use an Outlet Collection.
In you storyboard, drag from your first button and select Outlet Collection rather than Outlet as you would normally do.
Which gives youâ¦
@IBOutlet var buttons: [UIButton]!
Then connect all your other buttons to the same @IBOutlet
You can then say
buttons.forEach
$0.isHidden = true
In addition to @ukim's answer, you can use an Outlet Collection.
In you storyboard, drag from your first button and select Outlet Collection rather than Outlet as you would normally do.
Which gives youâ¦
@IBOutlet var buttons: [UIButton]!
Then connect all your other buttons to the same @IBOutlet
You can then say
buttons.forEach
$0.isHidden = true
answered Aug 10 at 20:18
Ashley Mills
25.4k778107
25.4k778107
add a comment |Â
add a comment |Â
up vote
4
down vote
You can also create Array
extension. It also make more sense to constraint element type to UIButton
, such that you can't call it for any other type of array.
Something like this,
extension Array where Element == UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
Then, using it like so,
[button1, button2, button3].hide() // hide buttons
[button1, button2, button3].show() // show
Extending collection makes more sense in this case, which gives more flexibility as the hide / show could be used with ArraySlices then.
Here is how you would do this,
extension Collection where Element: UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
func toggleVisibility()
forEach $0.isHidden = !$0.isHidden
And with this you can do some cool thing like,
// hide all but not first
myArrayOfButtons.dropFirst().hide()
// hide buttons in indexes 0 to 1
myArrayOfButtons[0 ... 1].hide()
// show all buttons but not last
myArrayOfButtons.dropLast().show()
// hide first 2 buttons
myArrayOfButtons.prefix(2).hide()
// show last button
myArrayOfButtons.suffix(1).show()
// toggle visibility of first 2
myArrayOfButtons.prefix(2).toggleVisibility()
add a comment |Â
up vote
4
down vote
You can also create Array
extension. It also make more sense to constraint element type to UIButton
, such that you can't call it for any other type of array.
Something like this,
extension Array where Element == UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
Then, using it like so,
[button1, button2, button3].hide() // hide buttons
[button1, button2, button3].show() // show
Extending collection makes more sense in this case, which gives more flexibility as the hide / show could be used with ArraySlices then.
Here is how you would do this,
extension Collection where Element: UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
func toggleVisibility()
forEach $0.isHidden = !$0.isHidden
And with this you can do some cool thing like,
// hide all but not first
myArrayOfButtons.dropFirst().hide()
// hide buttons in indexes 0 to 1
myArrayOfButtons[0 ... 1].hide()
// show all buttons but not last
myArrayOfButtons.dropLast().show()
// hide first 2 buttons
myArrayOfButtons.prefix(2).hide()
// show last button
myArrayOfButtons.suffix(1).show()
// toggle visibility of first 2
myArrayOfButtons.prefix(2).toggleVisibility()
add a comment |Â
up vote
4
down vote
up vote
4
down vote
You can also create Array
extension. It also make more sense to constraint element type to UIButton
, such that you can't call it for any other type of array.
Something like this,
extension Array where Element == UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
Then, using it like so,
[button1, button2, button3].hide() // hide buttons
[button1, button2, button3].show() // show
Extending collection makes more sense in this case, which gives more flexibility as the hide / show could be used with ArraySlices then.
Here is how you would do this,
extension Collection where Element: UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
func toggleVisibility()
forEach $0.isHidden = !$0.isHidden
And with this you can do some cool thing like,
// hide all but not first
myArrayOfButtons.dropFirst().hide()
// hide buttons in indexes 0 to 1
myArrayOfButtons[0 ... 1].hide()
// show all buttons but not last
myArrayOfButtons.dropLast().show()
// hide first 2 buttons
myArrayOfButtons.prefix(2).hide()
// show last button
myArrayOfButtons.suffix(1).show()
// toggle visibility of first 2
myArrayOfButtons.prefix(2).toggleVisibility()
You can also create Array
extension. It also make more sense to constraint element type to UIButton
, such that you can't call it for any other type of array.
Something like this,
extension Array where Element == UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
Then, using it like so,
[button1, button2, button3].hide() // hide buttons
[button1, button2, button3].show() // show
Extending collection makes more sense in this case, which gives more flexibility as the hide / show could be used with ArraySlices then.
Here is how you would do this,
extension Collection where Element: UIView
func show()
forEach $0.isHidden = false
func hide()
forEach $0.isHidden = true
func toggleVisibility()
forEach $0.isHidden = !$0.isHidden
And with this you can do some cool thing like,
// hide all but not first
myArrayOfButtons.dropFirst().hide()
// hide buttons in indexes 0 to 1
myArrayOfButtons[0 ... 1].hide()
// show all buttons but not last
myArrayOfButtons.dropLast().show()
// hide first 2 buttons
myArrayOfButtons.prefix(2).hide()
// show last button
myArrayOfButtons.suffix(1).show()
// toggle visibility of first 2
myArrayOfButtons.prefix(2).toggleVisibility()
edited Aug 11 at 16:43
answered Aug 10 at 20:15
Sandeep
14k54382
14k54382
add a comment |Â
add a comment |Â
up vote
3
down vote
You could also create an IBOutlet collection
@IBOutlet var multiButtons: [UIButton]!
Do do this: when you control-drag from the button to the code, select Outlet collection ; then control drag other buttons to this outlet collection.
Now, you can
for button in multiButtons
button.isHidden = true
add a comment |Â
up vote
3
down vote
You could also create an IBOutlet collection
@IBOutlet var multiButtons: [UIButton]!
Do do this: when you control-drag from the button to the code, select Outlet collection ; then control drag other buttons to this outlet collection.
Now, you can
for button in multiButtons
button.isHidden = true
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You could also create an IBOutlet collection
@IBOutlet var multiButtons: [UIButton]!
Do do this: when you control-drag from the button to the code, select Outlet collection ; then control drag other buttons to this outlet collection.
Now, you can
for button in multiButtons
button.isHidden = true
You could also create an IBOutlet collection
@IBOutlet var multiButtons: [UIButton]!
Do do this: when you control-drag from the button to the code, select Outlet collection ; then control drag other buttons to this outlet collection.
Now, you can
for button in multiButtons
button.isHidden = true
answered Aug 10 at 20:20
claude31
16016
16016
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51793357%2fhow-to-set-values-to-multiple-buttons-or-labels-at-once%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It seems like putting the buttons in an array makes sense.
â LAD
Aug 10 at 20:05