Swift - Retrieving subviews
Asked Answered
C

3

10

In my application I am adding labels to the view and then I am attempting to clear particular labels from the view when a button is clicked and am running into an error when trying to retrieve the subviews:

class FirstViewController: UIViewController {

     @IBAction func btnAddTask_Click(sender: UIButton){

          var subViews = self.subviews.copy()

    }
 }

I get the error:

'FirstViewController' does not have a member named 'subviews'

How can I get the subviews of the current view?

Cyclotron answered 28/9, 2014 at 14:58 Comment(3)
self.view.subviews?Sufficient
I have tried this and get this error: '[AnyObject]' does not have a member named 'copy'Cyclotron
Arrays always copy in Swift.Watanabe
W
14

UIViewController doesn't have a subviews property. It has a view property, which has a subviews property:

for subview in self.view.subviews {
   // Manipulate the view
}

But typically this is not a good idea. You should instead put the labels you want into an IBOutletCollection and iterate over that. Otherwise you've very tied to the exact set of subviews (which may change).

To create the IBOutletCollection, select all the labels you want in IB, and control-drag them to the source code. It should ask if you want to make a collection array. (Note that there is no promise on the order of this array.)

Watanabe answered 28/9, 2014 at 15:1 Comment(4)
thanks. This does work. I will look at your suggestion for using IBOutletCollectionCyclotron
I am dynamically creating the labels, so I'll need to figure out how to add the labels to the collection from the source code. Thanks againCyclotron
Just put them in an array property as you create them.Watanabe
Thanks, Rob, That's how I'm doing it.Cyclotron
R
11

For find all subviews from your view you can use this code:

for subview in self.view.subviews {
   // Use your subview as you want
}

But for use it, you must identify view what you need. You can mark any element, what you create, with special Identifier like this:

myButton.restorationIdentifier = "mySpecialButton";

And after that you can find your element use this structure:

for view in view.subviews {
                if (view.restorationIdentifier == "mySpecialButton") {
                    print("I FIND IT");
                    view.removeFromSuperview();
                }
            }

:)

Rudin answered 24/4, 2017 at 19:54 Comment(0)
C
0

Hacky elegance:

  1. Get the position of the click.
  2. Retrieve the view with hitTest.
  3. Differentiate the view with accessibilityIdentifier.

accessibilityIdentifier is developer facing and was intended for UI Automation.

Swift 4

@objc func handleTap(_ gestureRecognizer: UITapGestureRecognizer) {
    let positionInView = gestureRecognizer.location(in: view)
    let hitTestView = view?.hitTest(positionInView, with: nil)
    print("hitTestView: \(hitTestView?.accessibilityIdentifier)")
}
Cryptoclastic answered 1/9, 2019 at 2:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.