user979331 user979331 - 4 months ago 26
iOS Question

Swift loop through subviews and get the name for if condition

I have a loop like so:

for views in webview.scrollView.subviews
{
if(views.isKindOfClass(UIImageView))
{
}
}


inside the isKindOfClass condition, I would like to get the UIImageView image name and do another condition to see if the UIImageView is a certain image name. Something like this:

for views in webview.scrollView.subviews
{
if(views.isKindOfClass(UIImageView))
{
//if image name is equal to stamp.png
}
}


is this possible?

Answer

Not possible. You may want use accessibilityIdentifier instead:

for view in webview.scrollView.subviews {
    if let imageView = view as? UIImageView {
        if imageView.accessibilityIdentifier == "yourImageID" {
            // ...
        }
    }
}

You can define accessibility identifier for the UIImageView in Interface Builder.