Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

UIButton Identifier

How can I identify a UIButton element as a string and use that string in a function call? I want to be able to know what button has been pressed and then provide information based on that button that has been pressed. Would I use String Interpolation? I then want to use the string and run a query from the Foursquare API.

Below is the code I have been working on:

Firstly the button names:

let buttonNames = ["african", "buffet", "burger", "chinese", "fries", "grill", "icecream", "jap", "mex", "pizza", "seafood", "streetfood", "taj", "turkey"

The search term results:

var searchTerms = ["African", "All you can eat", "Buffet", "Burgers", "Brazilian", "Breakfast", "BBQ", "Chips", "Chinese", "Cakes", "Café", "Doughnuts", "Dessert", "English Breakfast", "Fast Food", "Fries", "French", "Grill", "Greek", "Italian", "Indian", "Japanese", "Jamaican", "Lebenese", "Mexican", "Pizza", "Street Food", "Sandwhich", "Turkish"]

Then the function call :

@IBAction func startSearchQuery() {

    if buttonNames == searchTerms {
        // Do Something
        var parameters = [Parameter.query:""]
        parameters += self.location.parameters()
        let searchTask = session.venues.search(parameters) {
            (result) -> Void in
            if let response = result.response {
                self.venues = response["venues"] as [JSONParameters]?
                self.tableView.reloadData()
            }
        }
        searchTask.start()
    } else {
        print("Search term not found!")
    }
}

If I can get some help on this I will be grateful. Thanks all!

1 Answer

Roland,

Sorry for the late reply.

My understanding of your issue is that you want to extract the button label text strings from any button that is tapped and use the string within your search query function. Is that correct?

  • My first thought is to ask if you have connected all of your buttons to the @IBAction func startSearchQuery() ? This will allow you to have one function that will work with all buttons when tapped.
  • My second thought would be to also pass sender: UIButton within the IBAction to allow you access to the text label in order to differentiate which button is tapped. This can be done with other ways like tags as well, but you want text right?
@IBAction func startSearchQuery(sender: UIButton) {

    if let buttonName = sender.titleLabel?.text {
       print(buttonName)
    }
}

Say I have two buttons named buttonOne and buttonTwo. With both of them connected to the IBAction, when I tap either of them, I will get a print out of their respective titleLabel name. You can now use that string in your query instead of printing it though.

Please let me know if this helps.

Regards,

Chris

Thanks I'll give it a try.