0

These are my Outlets, how can I out all off these in an array?

@IBOutlet weak var progressBar1: UIProgressView!
@IBOutlet weak var progressBar2: UIProgressView!
@IBOutlet weak var progressBar3: UIProgressView!
@IBOutlet weak var progressBar4: UIProgressView!

4 Answers4

3

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

Bobby
  • 176
  • 1
  • 4
  • Where would I put the array, when I place it somewhere in the code it gives me this Error. "Cannot use instance member 'progressBar1' within property initializer; property initializers run before 'self' is available" It does this for all of the progress views. – Kaleb Zimmerman Jan 09 '22 at 19:10
  • @kaleb outlet collection is a simple array, so you can access items using indexPath: `let progressBar1 = collection[0]`. – Bobby Jan 09 '22 at 19:40
2

On top declare a variable first like this

var outlets: [UIProgressView] = []

and now on ViewDidLoad method you can use this to put all outlets on that array

like this:

outlets = [progressBar1, progressBar2, progressBar3, progressBar4]

Hope you understand.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
  • Where would I put the array, when I place it somewhere in the code it gives me this Error. "Cannot use instance member 'progressBar1' within property initializer; property initializers run before 'self' is available" It does this for all of the progress views. – Kaleb Zimmerman Jan 09 '22 at 18:11
1
class ViewController: UIViewController {
    @IBOutlet weak var p1: UIProgressView!
    @IBOutlet weak var p2: UIProgressView!
    @IBOutlet weak var p3: UIProgressView!
    @IBOutlet weak var p4: UIProgressView!
    
    var outlets: [UIProgressView] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        outlets = [
            p1,p2,p3,p4
        ]
    }
}

If you have other types of views you can use

var outlets: [UIView] = [...]
CloudBalancing
  • 1,461
  • 2
  • 11
  • 22
  • Where would I put the array, when I place it somewhere in the code it gives me this Error. "Cannot use instance member 'progressBar1' within property initializer; property initializers run before 'self' is available" It does this for all of the progress views. – Kaleb Zimmerman Jan 09 '22 at 19:10
  • Both Faysal and Bobby answers cover it - but I updated my example with code that compiles with no error just in case (: – CloudBalancing Jan 09 '22 at 20:39
  • ok, thank you very much. – Kaleb Zimmerman Jan 09 '22 at 21:02
1

As mentioned here Swift - IBOutletCollection equivalent you can use IBOutletCollection to do that. You can drag all your views to one IBOutlet array.

@IBOutlet weak var progressBars: [UIProgressView]!

For example, you can access the first progressBar like

progressBars[0]

But you have to careful about the order of progressBars, when you define IBOutletCollections the collection will not be order guaranteed. You can define the for each view and sort by their tags in runtime as mentioned here also Is IBOutletCollection guaranteed to be of correct order?

To order all views by their tags like

progressBars = progressBars.sorted { $0.tag < $1.tag }
Vural Çelik
  • 43
  • 1
  • 7