• <tfoot id='MIjnJ'></tfoot>

    • <bdo id='MIjnJ'></bdo><ul id='MIjnJ'></ul>

    <small id='MIjnJ'></small><noframes id='MIjnJ'>

    <i id='MIjnJ'><tr id='MIjnJ'><dt id='MIjnJ'><q id='MIjnJ'><span id='MIjnJ'><b id='MIjnJ'><form id='MIjnJ'><ins id='MIjnJ'></ins><ul id='MIjnJ'></ul><sub id='MIjnJ'></sub></form><legend id='MIjnJ'></legend><bdo id='MIjnJ'><pre id='MIjnJ'><center id='MIjnJ'></center></pre></bdo></b><th id='MIjnJ'></th></span></q></dt></tr></i><div id='MIjnJ'><tfoot id='MIjnJ'></tfoot><dl id='MIjnJ'><fieldset id='MIjnJ'></fieldset></dl></div>
    1. <legend id='MIjnJ'><style id='MIjnJ'><dir id='MIjnJ'><q id='MIjnJ'></q></dir></style></legend>

        通过发件人在 Swift 上获取按钮按下的 id

        时间:2023-07-09
      1. <tfoot id='vE5ZR'></tfoot>
          <tbody id='vE5ZR'></tbody>
      2. <legend id='vE5ZR'><style id='vE5ZR'><dir id='vE5ZR'><q id='vE5ZR'></q></dir></style></legend>

          <small id='vE5ZR'></small><noframes id='vE5ZR'>

          <i id='vE5ZR'><tr id='vE5ZR'><dt id='vE5ZR'><q id='vE5ZR'><span id='vE5ZR'><b id='vE5ZR'><form id='vE5ZR'><ins id='vE5ZR'></ins><ul id='vE5ZR'></ul><sub id='vE5ZR'></sub></form><legend id='vE5ZR'></legend><bdo id='vE5ZR'><pre id='vE5ZR'><center id='vE5ZR'></center></pre></bdo></b><th id='vE5ZR'></th></span></q></dt></tr></i><div id='vE5ZR'><tfoot id='vE5ZR'></tfoot><dl id='vE5ZR'><fieldset id='vE5ZR'></fieldset></dl></div>

              • <bdo id='vE5ZR'></bdo><ul id='vE5ZR'></ul>
                1. 本文介绍了通过发件人在 Swift 上获取按钮按下的 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我有一个带有 3 个按钮的情节提要

                  有没有办法为每个按钮获取某种标识符?

                  顺便说一句,它们是图像,所以它们没有标题.

                  @IBAction func mainButton(sender: UIButton) {打印(发件人)}

                  解决方案

                  您可以在情节提要中为每个按钮设置一个 tag.然后你可以这样识别它们:

                  @IBAction func mainButton(sender: UIButton) {println(sender.tag)}

                  为了提高可读性,您可以定义一个枚举,其值对应于所选标签.因此,如果您为按钮设置 012 等标签,则可以在类声明上方执行以下操作:

                  枚举 SelectedButtonTag: Int {案例一案例二案例三}

                  然后您将拥有以下内容,而不是处理硬编码的值:

                  @IBAction func mainButton(sender: UIButton) {切换发件人.tag {案例 SelectedButtonTag.First.rawValue:println("当第一个按钮被点击时做一些事情")案例 SelectedButtonTag.Second.rawValue:println("当第二个按钮被点击时做一些事情")案例 SelectedButtonTag.Third.rawValue:println("当第三个按钮被点击时做某事")默认:println("默认")}}

                  So I have a storyboard with 3 buttons I want to just create 1 action for all those 3 buttons and decide what to do based on their label/id...

                  Is there a way to get some kind of identifier for each button?

                  By the way they are images, so they don't have a title.

                  @IBAction func mainButton(sender: UIButton) {
                      println(sender)
                  }
                  

                  解决方案

                  You can set a tag in the storyboard for each of the buttons. Then you can identify them this way:

                  @IBAction func mainButton(sender: UIButton) {
                      println(sender.tag)
                  }
                  

                  EDIT: For more readability you can define an enum with values that correspond to the selected tag. So if you set tags like 0, 1, 2 for your buttons, above your class declaration you can do something like this:

                  enum SelectedButtonTag: Int {
                      case First
                      case Second
                      case Third
                  }
                  

                  And then instead of handling hardcoded values you will have:

                  @IBAction func mainButton(sender: UIButton) {
                      switch sender.tag {
                          case SelectedButtonTag.First.rawValue:
                              println("do something when first button is tapped")
                          case SelectedButtonTag.Second.rawValue:
                              println("do something when second button is tapped")
                          case SelectedButtonTag.Third.rawValue:                       
                              println("do something when third button is tapped")
                          default:
                              println("default")
                      }
                  }
                  

                  这篇关于通过发件人在 Swift 上获取按钮按下的 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何以编程方式将触摸事件伪造为 UIButton? 下一篇:为 uibutton ios 使用表格视图披露指示器样式

                  相关文章

                    <bdo id='Frbc5'></bdo><ul id='Frbc5'></ul>
                2. <legend id='Frbc5'><style id='Frbc5'><dir id='Frbc5'><q id='Frbc5'></q></dir></style></legend>

                    <i id='Frbc5'><tr id='Frbc5'><dt id='Frbc5'><q id='Frbc5'><span id='Frbc5'><b id='Frbc5'><form id='Frbc5'><ins id='Frbc5'></ins><ul id='Frbc5'></ul><sub id='Frbc5'></sub></form><legend id='Frbc5'></legend><bdo id='Frbc5'><pre id='Frbc5'><center id='Frbc5'></center></pre></bdo></b><th id='Frbc5'></th></span></q></dt></tr></i><div id='Frbc5'><tfoot id='Frbc5'></tfoot><dl id='Frbc5'><fieldset id='Frbc5'></fieldset></dl></div>

                    <small id='Frbc5'></small><noframes id='Frbc5'>

                    <tfoot id='Frbc5'></tfoot>