Swift put multiple IBOutlets in an Array

31,866

Solution 1

you can define a generic outlet collection in Swift like this:

@IBOutlet var collectionOfViews: Array<UIView>? // = [UIView]?

or for e.g. UIButton objects:

@IBOutlet var collectionOfButtons: Array<UIButton>? // = [UIButton]?

you can find your collections under the Outlet Collections group as usually are in the File's Owner:

Outlet Collections

it would look on my console after connecting 5 random buttons:

Connected UIButton instances to Collection

Solution 2

Follow these steps to create an array of outlets an connect it with IB Elements:

  • Create an array of IBOutlets
  • Add multiple UIElements (Views) in your Storyboard ViewController interface
  • Select ViewController (In storyboard) and open connection inspector
  • There is option 'Outlet Collections' in connection inspector (You will see an array of outlets there)
  • Connect if with your interface elements

-

class ViewController2: UIViewController {


    @IBOutlet var collection:[UIView]!


    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

enter image description here

Solution 3

Solution here Swift - IBOutletCollection equivalent

@IBOutlet var objectCollection: [Object]

Solution 4

This is for macOS (should be similar for iOS) and I do not find an "Outlet Collections" in my storyboard (looks like they took that option out). So I put all my buttons in an NSStackView and linked the stack from storyboard

@IBOutlet weak var buttons: NSStackView!

and then I looped over them to make changes accordingly

for case let (index, button as NSButton) in buttons.arrangedSubviews.enumerated() {
            if(index + 1 != someButtonIndex) {button.state = .off}
            else {button.state = .on}
        }

you can also use tag instead of index

Share:
31,866
cor
Author by

cor

Updated on August 06, 2021

Comments

  • cor
    cor almost 3 years

    Multiple IBOutlets

    I made these (marked with red border) IBOutlets using ctrl + drag

    But i don't like to have the exact same line 9 times (DRY)

    How do i put these IBOutlets in an Array?

  • cor
    cor almost 10 years
    How do i connect the buttons from the storybord to this array?
  • holex
    holex almost 10 years
    @CorPruijs, you can find your collection under the Outlet Collections group. you can connect the multiple items to the collection as usually you'd do in the case of one outlet.
  • cor
    cor almost 10 years
    I keep getting a "not initialized at super.init call" error, i have tried "@IBOutlet var levelField: Array<UIButton> = Array<UIButton>" and "@IBOutlet var levelField: Array<UIButton> = UIButton" and using a for loop but nothing worked
  • holex
    holex almost 10 years
    @CorPruijs, is that your code, @IBOutlet var levelField: Array<UIButton> = Array<UIButton>...? you don't need the right size of the operand, please chuck everything away after the = symbol including the = too; please see my answer, I have not assigned anything to it, because it is needless.
  • cor
    cor almost 10 years
    That's the first thing i tried off course, but it didn't work. <br> Then i tried using the = Array<UIButton> thingy because i needed "= UIButton()" when i did a single button
  • holex
    holex almost 10 years
    @CorPruijs, I don't know what else I can do, that works for me well but I'm not seeing your code – therefore I have no idea where the procedure is derailed.
  • cor
    cor almost 10 years
  • redent84
    redent84 about 9 years
    Change @IBOutlet var collectionOfViews: Array<UIView> to @IBOutlet var collectionOfViews: Array<UIView>? to avoid compilation errors.
  • holex
    holex about 9 years
    @redent84, thanks, unfortunately the Swift language is still done after a year, and evolves continuously.
  • Jay Imerman
    Jay Imerman over 8 years
    As of Swift 2 (Xcode 7.1), this does not work as shown above. I added the IBOutlet array, and connected the array to the buttons in Interface Builder. But when I try to compile, I get a compiler error "Class ViewController has no initializers". The fix suggested is to initialize the array, so: @IBOutlet var buttonArray: [UIButton] = []
  • Jay Imerman
    Jay Imerman over 8 years
    This answer is way too brief, provides no details, and should be removed.
  • ICL1901
    ICL1901 over 7 years
    or, perhaps the writer can expand on their answer
  • SomaMan
    SomaMan about 7 years
    The answer only needs to be brief
  • coolly
    coolly almost 7 years
    there is no weak in that example. Does it creating a reference cycle?
  • holex
    holex almost 7 years
    the collections (a.k.a. Array<UIView>) must be kept alive by a strong reference, there is no weak modifier here.
  • Jamshed Alam
    Jamshed Alam over 6 years
    How i can make work it without array ? @IBOutlet weak var backBtn :UIButton!, refreshBtn :UIButton!, searchBtn:UIButton!, playBtn:UIButton!, cancelDownloadBtn:UIButton!
  • holex
    holex over 6 years
    @JamshedAlam, outlet collections are always arrays; if you drop the array, it won't be an outlet collection anymore but single individual outlets only.
  • Happiehappie
    Happiehappie almost 6 years
    Can anyone explain why is the collection outlets not a weak but strong? Is it because it's an array?
  • holex
    holex almost 6 years
    @Happiehappie, your assumption is correct: you need to keep the array alive.