How to identify which subView has been pressed in Swift?

subview in swift
find subview
swift get subview by id
swift reorder subviews
swift check subview type
find button with tag swift
add subview on top swift
subview not showing swift

I have a built a UIView class called SetView. In its initializer I create multiple subviews and later in ViewController I want to determine which subView has been pressed. in my viewDidLoad method I iterate through all the subviews add them to a class array of UIView called mySubViews and it my getIndex method, I am trying to retrieve the value which is always retrieved as nil. I suppose that it is my main view that is passed as a sender rather than particular subviews but I don't know how to pass specific subviews since #selector does not accept argument. I would appreciate any suggestions on how I could determine which subview was pressed to update features of a given subview.

override func viewDidLoad() {
    super.viewDidLoad()

    for view in setView.subviews {
        mySubViews.append(view)
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(getIndex(_:)))
        gestureRecognizer.delegate = self
        view.addGestureRecognizer(gestureRecognizer)
    }
}

@objc func getIndex(_ sender:UIView) {
    print(mySubViews.index(of: sender))
}

The sender should be the gesture recognizer. Then give your views a tag and set the same tag for your gesture recognizer. Then you can get the view with viewWithTag.

Or with your array it could be like

override func viewDidLoad() {
   super.viewDidLoad()

   var index = 0

   for view in setView.subviews {
       mySubViews.append(view)
       let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(getIndex(_:)))
       gestureRecognizer.tag = index
       gestureRecognizer.delegate = self
       view.addGestureRecognizer(gestureRecognizer)

       index += 1
   }
}

@objc func getIndex(_ sender: UITapGestureRecognizer) {
    print(mySubViews[sender.tag])
}

How to identify which subView has been pressed in Swift?, I have a built a UIView class called SetView. In its initializer I create multiple subviews and later in ViewController I want to determine which subView has been  If you need a quick way to get hold of a view inside a complicated view hierarchy, you're looking for viewWithTag() – give it the tag to find and a view to search from, and this method will search all subviews, and all sub-subviews, and so on, until it finds a view with the matching tag number.

The sender in your target method is a gesture recognizer. The fact that it is only interpreted as UIView will always return nil in your call.

Try the following:

@objc func getIndex(_ sender: UIGestureRecognizer) {
    print(mySubViews.index(of: sender.view))
}

Still I would prefer you would use a single gesture recognizer on the super view. Then you can check the hit view by checking if the gesture recognizer was within the view bounds:

@objc func getIndex(_ sender: UIGestureRecognizer) {
    let allViewsAtGestureLocation = mySubViews.filter { $0.bounds.contains(sender.location(in: $0)) }
    let firstHitView = mySubViews.first(where: { $0.bounds.contains(sender.location(in: $0)) })
}

I assume you would need the second one. From it you can again find an index.

How to identify which subView has been pressed in Swift?, In its initializer I create multiple subviews and later in ViewController I want to determine which subView has been pressed. in my viewDidLoad method I iterate​  Add Subview and Remove subview example in Swift In this video I am going to show you how to add custom user interface as a subview and how to remove it from super view on a button tap event. Also, in this video I am using the project I have created earlier.

Maybe try something like this:

for view in setView.subviews {
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(getIndex(_:)))
    gestureRecognizer.delegate = self
    view.addGestureRecognizer(gestureRecognizer)
    mySubViews.addSubview(view)
}

Don't use append if you're trying to add it in, and add it after you assign the gesture.

Check if button is pressed swift, This component has a label and generates an event when pressed. 0 Buttons, How do i make it so i know how many times that button has been pressed Oct controller over all the subviews in parent view controller including navigation bar. How to bring a subview to the front of a UIView. Swift version: 5.0. Paul Hudson @twostraws May 28th 2019. UIKit draws views back to front, which means that views higher up the stack are drawn on top of those lower down. If you want to bring a subview to the front, there's a method just for you: bringSubviewToFront().

How to find a UIView subview using viewWithTag(), You see, if you use viewWithTag() to find some specific nested view then you're effectively saying “there's a view that I have given a magic  Subview and SuperView Functions in Swift4 Emre Çolak. Rating is available when the video has been rented. iOS Swift Tutorial: Brian's Advent Calendar

How to detect when the Back button is tapped, You probably already know that viewWillDisappear() is called when a view controller is about to go away, and that's also called when the user  In this video I demonstrate how to add several images as subviews to UIScrollView in Swift. In my example I use UIImageView but it can be a custom user interface design ex. XIB file. To learn how

Detect Long Press Gesture iOS Tutorial, Long-press gestures are a type of a continuous gesture. UIKit will detect if a press is pushed long instead of a single tap. In this tutorial a  Custom user interface with XIB as Subview in Swift. Hide and Remove. Rating is available when the video has been rented. Swift 4.2, Tutorial : How to

Comments
  • You don't need to add gesture to every of your subviews, add gesture in yous main view only. And from tap gesture sender you can get which view is tapped.
  • @iPeter what property of tap gesture should I use to retrieve which tap gesture sender was pressed? I have looked at the hash values and indeed they are different for different subviews clicked. However, they change with every compilation of the app so I cannot use them to identify specific subViews
  • Can you please just try getIndex(_ sender:UIGestureRecognizer) { print(mySubViews.index(of: sender.view))
  • Use only One tap gesture. and from sender.view, you can get the view that is tapped.
  • I believe you could do the same without tags and just use sender.view.
  • @MaticOblak You are right. I didn't want to post something I didn't tested to be sure it will work. Thanks for clarifying.
  • It still gives nil after trying it.