Swift put multiple IBOutlets in an Array
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:
it would look on my console after connecting 5 random buttons:
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()
}
}
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
cor
Updated on August 06, 2021Comments
-
cor almost 3 years
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 almost 10 yearsHow do i connect the buttons from the storybord to this array?
-
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 almost 10 yearsI 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 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 almost 10 yearsThat'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 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 almost 10 yearsLet us continue this discussion in chat.
-
redent84 about 9 yearsChange
@IBOutlet var collectionOfViews: Array<UIView>
to@IBOutlet var collectionOfViews: Array<UIView>?
to avoid compilation errors. -
holex about 9 years@redent84, thanks, unfortunately the Swift language is still done after a year, and evolves continuously.
-
Jay Imerman over 8 yearsAs 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 "ClassViewController
has no initializers". The fix suggested is to initialize the array, so:@IBOutlet var buttonArray: [UIButton] = []
-
Jay Imerman over 8 yearsThis answer is way too brief, provides no details, and should be removed.
-
ICL1901 over 7 yearsor, perhaps the writer can expand on their answer
-
SomaMan about 7 yearsThe answer only needs to be brief
-
coolly almost 7 yearsthere is no weak in that example. Does it creating a reference cycle?
-
holex almost 7 yearsthe collections (a.k.a.
Array<UIView>
) must be kept alive by a strong reference, there is noweak
modifier here. -
Jamshed Alam over 6 yearsHow i can make work it without array ? @IBOutlet weak var backBtn :UIButton!, refreshBtn :UIButton!, searchBtn:UIButton!, playBtn:UIButton!, cancelDownloadBtn:UIButton!
-
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 almost 6 yearsCan anyone explain why is the collection outlets not a weak but strong? Is it because it's an array?
-
holex almost 6 years@Happiehappie, your assumption is correct: you need to keep the array alive.