How to set values to multiple buttons or labels at once?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
9
down vote

favorite
1












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.







share|improve this question






















  • It seems like putting the buttons in an array makes sense.
    – LAD
    Aug 10 at 20:05














up vote
9
down vote

favorite
1












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.







share|improve this question






















  • It seems like putting the buttons in an array makes sense.
    – LAD
    Aug 10 at 20:05












up vote
9
down vote

favorite
1









up vote
9
down vote

favorite
1






1





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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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






share|improve this answer



























    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.



    enter image description here



    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






    share|improve this answer



























      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()





      share|improve this answer





























        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






        share|improve this answer




















          Your Answer





          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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






          share|improve this answer
























            up vote
            6
            down vote



            accepted










            Put them in an array and iterate through the array.



            [button1, button2, button3].forEach 
            $0.isHidden = true






            share|improve this answer






















              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






              share|improve this answer












              Put them in an array and iterate through the array.



              [button1, button2, button3].forEach 
              $0.isHidden = true







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 10 at 20:01









              ukim

              1,415615




              1,415615






















                  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.



                  enter image description here



                  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






                  share|improve this answer
























                    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.



                    enter image description here



                    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






                    share|improve this answer






















                      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.



                      enter image description here



                      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






                      share|improve this answer












                      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.



                      enter image description here



                      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







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 10 at 20:18









                      Ashley Mills

                      25.4k778107




                      25.4k778107




















                          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()





                          share|improve this answer


























                            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()





                            share|improve this answer
























                              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()





                              share|improve this answer














                              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()






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Aug 11 at 16:43

























                              answered Aug 10 at 20:15









                              Sandeep

                              14k54382




                              14k54382




















                                  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






                                  share|improve this answer
























                                    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






                                    share|improve this answer






















                                      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






                                      share|improve this answer












                                      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







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 10 at 20:20









                                      claude31

                                      16016




                                      16016






















                                           

                                          draft saved


                                          draft discarded


























                                           


                                          draft saved


                                          draft discarded














                                          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













































































                                          這個網誌中的熱門文章

                                          How to combine Bézier curves to a surface?

                                          Carbon dioxide

                                          Why am i infinitely getting the same tweet with the Twitter Search API?