Swift: Get all subviews of a specific type and add to an array

后端 未结 11 766
Happy的楠姐
Happy的楠姐 2020-12-13 12:25

I have a custom class of buttons in a UIView that I\'d like to add to an array so that they\'re easily accessible. Is there a way to get all subviews of a specific class and

11条回答
  •  悲哀的现实
    2020-12-13 12:50

    I can't test it right now but this should work in Swift 2:

    view.subviews.flatMap{ $0 as? YourView }
    

    Which returns an array of YourView

    Here's a tested, typical example, to get a count:

    countDots = allDots!.view.subviews.flatMap{$0 as? Dot}.count
    

提交回复
热议问题