Swift Put Multiple Iboutlets in an Array

How can I add make an array for all of my IBOutlets?

Open the Assistant Editor, right-click and drag from one of your UIProgressView's or just drag from its "Referencing Outlet Collections" to the code file.
Insert outlet collection

Then you can drag from your swift file's @IBOutlet to the rest of your UIProgressView's. Add view to collection

Put multiple UILabels into an array

As rmaddy mentioned in a comment you can use an outlet collection:

@IBOutlet private var labels: [UILabel]!

Then in your storyboard labels will show up under Outlet Collections when right-clicking your ViewController, and you can link multiple labels:

Sample Image

How to make IBOutlets out of an array of objects?

It is possible, it’s called outlet collection. This is the way to define an outlet collection:

@property(retain) IBOutletCollection(UIImageView) NSArray *images;

Now you can stick more than one object into the outlet in the Interface Builder, the array will be created for you when the interface is loaded.

Warning whilst putting IBOutlet buttons in an array

Either use lazy var declaration for allSpaces or make an @IBOutlet collection:

lazy var allSpaces = [oneOne,twoOne,threeOne,oneTwo,twoTwo,threeTwo,oneThree,twoThree,threeThree]

or:

@IBOutlet weak var buttons: [UIButton]! // Connect your outlets here.

Array of IBOutlets in Swift

@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var countryField: UITextField!
@IBOutlet weak var cityField: UITextField!

...

let array = [emailField, countryField, cityField]

Have a look at the documentation on collection types

edit: you can split the declaration/init of that array

// outside any method
var array = [UITextField]?

// in viewDidLoad
self.array = [emailField, countryField, cityField]


Related Topics



Leave a reply



Submit