将字符串属性添加到Swift中的UIButton [英] Add a string property to a UIButton in Swift

查看:398
本文介绍了将字符串属性添加到Swift中的UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swift中将字符串属性与 UIButton 相关联?我不希望字符串显示为按钮文本,只是将其指定为按钮作为标识符或键。以下是我到目前为止:

How can I associate a string property with a UIButton in Swift? I don't want the string to appear as the button text, simply to be assigned to the button as an identifier or a key. Here is what I have so far:

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)
}

所以我想我需要一些东西

So I think I need something after

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

将标识符分配给按钮。

To assign the identifier to the button.

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

The reason for this is I'm trying to implement a decision tree logic so that I can keep track of each answer button that is tapped to generate a code string that will correspond to a final result.

推荐答案

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

Using the Objective-C runtime, we can add properties to classes at runtime:

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 还允许我们设置 descriptiveName 通过Interface Builder获取属性。

Adding @IBInspectable also lets us set the descriptiveName property through Interface Builder.

有关Objective-C运行时的更多信息,我建议您查看这篇NSHipster文章

For more about the Objective-C runtime, I recommend you check out this NSHipster article.

这篇关于将字符串属性添加到Swift中的UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆