【问题标题】:Add a string property to a UIButton in Swift将字符串属性添加到 Swift 中的 UIButton
【发布时间】:2015-05-13 12:17:18
【问题描述】:

如何在 Swift 中将字符串属性与 UIButton 关联?我不希望字符串显示为按钮文本,只是作为标识符或键分配给按钮。这是我到目前为止所拥有的:

func createAnswerButtons() {

    var index:Int
    for index = 0; index < self.currentQuestion?.answers.count; index++ {

        // Create an answer button view
        var answer:AnswerButtonView = AnswerButtonView()
        selection.setTranslatesAutoresizingMaskIntoConstraints(false)

        // Place into content view
        self.scrollViewContentView.addSubview(answer)

        // Add a tapped gesture recognizer to the button
        let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("answerTapped:"))
        answer.addGestureRecognizer(tapGesture)

        // Add constraints etc

        // Set the answer button text
        let answerText = self.currentQuestion!.answers[index]
        answer.setAnswerText(answerText)

        // Set the identifier for each answer button
        self.identifier = self.currentQuestion!.answerIdentifier[index]

        // Add to the selection button array
        self.answerButtonArray.append(answer)
}

所以我想我之后需要一些东西

// Set the identifier for each answer
        self.identifier = self.currentQuestion!.answerIdentifier[index]

将标识符分配给按钮。

这样做的原因是我正在尝试实现决策树逻辑,以便我可以跟踪每个被点击的答案按钮以生成与最终结果相对应的代码字符串。

【问题讨论】:

  • 为什么不用tag属性呢?
  • @LeonardoSavioDabus 标签不是字符串。这是NSInteger
  • @nhgrif 我知道如果他不显示文本,我认为不需要字符串
  • brad88,你试过我的答案了吗?
  • 不,我不知道如何利用那个答案!尝试了几次,但老实说我不确定我在做什么

标签: ios swift uibutton


【解决方案1】:

使用 Objective-C 运行时,我们可以在运行时向类添加属性:

extension UIButton {
    private struct AssociatedKeys {
        static var DescriptiveName = "nsh_DescriptiveName"
    }

    @IBInspectable var descriptiveName: String? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.DescriptiveName) as? String
        }
        set {
            if let newValue = newValue {
                objc_setAssociatedObject(
                    self,
                    &AssociatedKeys.DescriptiveName,
                    newValue as NSString?,
                    UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                )
            }
        }
    }
}

添加@IBInspectable 还可以让我们通过Interface Builder 设置descriptiveName 属性。

有关 Objective-C 运行时的更多信息,我建议您查看this NSHipster article

【讨论】:

  • 这是一个很好的解决方案,但它非常老实说 OP 不需要所有 UIButton 实例来包含此属性。无论如何都要上船。
【解决方案2】:

如果您要识别的按钮应具有唯一标识符(如果您正在编写 UI 测试,这很重要),您可以使用可访问性标识符 (button.accessibilityIdentifier)。

您还可以继承 UIButton 并添加变量buttonIdentifier

class IdentifiedButton: UIButton {
    var buttonIdentifier: String?
}

【讨论】:

  • 你能否提供更多关于这方面的信息,因为我完全被困住了!
  • 我没有 UIButton,因为我以编程方式创建了按钮视图。我创建了一个名为 AnswerButtonView 的类,现在在视图控制器中我正在创建按钮视图
【解决方案3】:

您可以使用 UIButton 的 accessibilityIdentifier 属性。

@IBOutlet weak var button: UIButton!
button.accessibilityIdentifier = "Some useful text"

【讨论】:

    【解决方案4】:

    使用

    button.accessibilityIdentifier = "some text"
    

    不是标签。

    【讨论】:

      【解决方案5】:

      您可以使用要与按钮关联的字符串创建一个数组。然后将按钮标签设置为要与按钮关联的字符串的索引。因此:

      var myStrings = ["First","Second","Third"]
      
      button.tag = //insert a number corresponding to the string index in myStrings that you want for the button
      
      func buttonPressed(sender: UIButton){
          var selectedString = myString[sender.tag]
      }
      

      【讨论】:

      • 这个怎么用?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 2011-10-10
      相关资源
      最近更新 更多