识别一个在swift中被按下的UIB按钮?[英] Identify a UIButton that is pressed in swift?

本文是小编为大家收集整理的关于识别一个在swift中被按下的UIB按钮?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何将Uibutton元素识别为字符串并在函数调用中使用该字符串?我希望能够知道已按下哪个按钮,然后根据按下的该按钮提供信息.我会使用字符串插值吗?然后,我想使用字符串并从foursquare api运行查询.

以下是我一直在处理的代码:

首先是按钮名称:

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

搜索词结果:

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"]

然后函数调用:

@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!")
    }
}

如果我能为此获得一些帮助,我将感谢.谢谢大家!

推荐答案

在按钮上放一个标签,您可以以编程方式或属性检查器进行此标签,默认标签为0

button.tag = index

@IBAction func startSearchQuery(sender: AnyObject) {

 let button = sender as! UIButton
 let index = button.tag
 let buttonName = buttonNames[index]
  ....
}

其他推荐答案

创建两个按钮和搜索术语.

@IBAction func startSearchQuery(sender: UIButton)
{
    let searchTerm = searchTerms[buttons.indexOf(sender)
    //do your staff
}

或者您可以设置/获取按钮的可访问性标签.

其他推荐答案

  @IBAction func myButton(_ sender: UIButton?) {
             let button = sender
             if button?.allControlEvents != .touchUpInside {
                 print("button not pressed from the button")
             } else {
                 print("button pressed from the button")
             }
        }

本文地址:https://www.itbaoku.cn/post/2090867.html

问题描述

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!

推荐答案

Put a tag on the button, you can do this programmatically or on the attribute inspector, the default tag is 0

button.tag = index

@IBAction func startSearchQuery(sender: AnyObject) {

 let button = sender as! UIButton
 let index = button.tag
 let buttonName = buttonNames[index]
  ....
}

其他推荐答案

Create two arrays of buttons and search terms.

@IBAction func startSearchQuery(sender: UIButton)
{
    let searchTerm = searchTerms[buttons.indexOf(sender)
    //do your staff
}

Or you can set/get accessibility labels for buttons.

其他推荐答案

  @IBAction func myButton(_ sender: UIButton?) {
             let button = sender
             if button?.allControlEvents != .touchUpInside {
                 print("button not pressed from the button")
             } else {
                 print("button pressed from the button")
             }
        }