1

I have an outlet collection of DayViews's which are custom UIView objects. In my 'ClientTaskVC' I want to be able to reference DayView and be able to reach in and get its UIElements, each DayView has a dayLabel, dateLabel and checkmarkImg. I'm not sure if putting all the labels/imageviews in outlet collections is a smart idea as it would be harder to track and it looks wrong. In my first image I've detailed how this would look in code as well as showing my View controller with all my views as reference.

First Image: outletcollections

What I tried to do was add the outlets to the actual DayView custom class for all the labels and image but when I connected the outlets it could only connect to one DayView and was not reusable for every single DayView object on the screen, I don't think the right approach is to create an outlet collection in the custom DayView class to reference each of the views labels either so now I'm not really sure how to approach this problem

Second Image: outletsDayViewClass

Ideally what I want is to just have my outlet collection in 'ClientTaskVC':

@IBOutlet var dayViews: [DayView]!

and call its UIElements (ex: dayViews[i].dayLbl.text) like so:

for i in 0 ..< dayViews.count {
        let date = Date().addingTimeInterval(TimeInterval(86400 * i))
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd"

        // See how the dayViews label, 'dayLbl' is being referenced
        dayViews[i].dayLbl.text = dateFormatter.string(from: date)
        dayViews[i].dayLbl.textAlignment = .center
    }

Any help on how to do this with outlet collections would be greatly appreciated. Thank you.

Jumpman987
  • 307
  • 1
  • 5
  • 17
  • Possible duplicate of [Can't link to outlet collection in XCode9](https://stackoverflow.com/questions/46384857/cant-link-to-outlet-collection-in-xcode9) – Kevin DiTraglia Nov 28 '17 at 21:39
  • Looks like a case for a UICollectionView to me. – Au Ris Nov 28 '17 at 22:13
  • @AuRis The reason why I didn't use UICollectionView is because there is only ever going to be 7 views, never more or less. I was told that was the most optimal approach. – Jumpman987 Nov 28 '17 at 22:29
  • @Jumpman987 did you see the question I linked? It's a bug in xcode9 I submitted it to apple, but the work-around is you drag the outlet to the element, instead of the element to the outlet. For some reason this uses cmd+click instead of ctrl+click. – Kevin DiTraglia Nov 28 '17 at 22:33
  • @KevinDiTraglia What I'm trying to do is iterate through my dayView outlet collection and get the labels/image on the DayView custom object, I can connect an outlet collection just fine but I can't use the 'dayLbl', 'checkMarkImg' and 'dateLbl' from any of the outlet collection indexes. If I added the outlets for all the labels and such to the DayView file it will only set those outlets for one specific view, not all them, therefore I can't iterate through them to display the info I want. – Jumpman987 Nov 28 '17 at 23:11
  • Check my answer, and let me know if it works for you. – Xcoder Jan 23 '18 at 22:25
  • If my answer works for you, you can click the check mark to accept so that others with the same problem can be directed to my answer. – Xcoder Mar 14 '18 at 22:53
  • No, the answer didn't solve my problem. Otherwise I would've checked it already... – Jumpman987 Mar 14 '18 at 23:22
  • @Jumpman987 If there is anything wrong with my answer, feel free to ask any questions :) – Xcoder Nov 26 '18 at 22:12

1 Answers1

1

Connect the whole view as an outlet:

@IBOutlet weak var dayView: UIView!

You could loop through all of DayView's subviews like so:

var dayViewSubviews: UIView = []

for subView in dayView {
    dayViewSubviews.append(subView)
}
Xcoder
  • 1,433
  • 3
  • 17
  • 37