ahmed
ahmed

Reputation: 105

Getting tag or index for a tap gesture recognizer

I have a uitapgesturerecognizer, for a uiview. I have 30 of these uiviews in a scrollview. Also in each of these views there is a label with different words in it for each view. Now when I hit a tap gesture recognizer, I want to access the text from the label for that particular view. Whats happening is I'm printing the words of the last view in the index. Can someone show me how I can access each view in each index.

open var Name: UILabel!
open func ynCategoryButtonClicked(_ sender: UITapGestureRecognizer) {
    guard let text = Name.text else { return }
    ynSearch.appendSearchHistories(value: text)
    self.delegate?.ynCategoryButtonClicked(text: text)
}

Upvotes: 1

Views: 727

Answers (2)

Zohaib Hassan
Zohaib Hassan

Reputation: 984

The place where you are assigning your tapGesture to your "UIButtonView", add this line:

yourButton.accessibilityHint = "YourText"

And In your "buttonTapped()" method get the text like this:

let yourText = "\(sender.accessibilityHint)"

Hope this will help!

Upvotes: 2

Benhamine
Benhamine

Reputation: 153

Honestly, it sounds like you should be using a UITableView. Either way though, you should alter your delegate method ynCategoryButtonClicked to also pass a reference to itself (so something like delegate?.ynCategoryButtonClicked(text: text, ynCategory: self)). Then if you go the TableView route you can find the index by using indexPath(for:UITableViewCell). Or, if you stick with the scrollview, you should be able to maintain an array of the views, and just search through them for the ynCategory.

Upvotes: 0

Related Questions