
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]
将标识符分配给按钮.
这样做的原因是我正在尝试实现一个决策树逻辑,这样我就可以跟踪每个被点击的答案按钮,以生成一个与最终结果相对应的代码字符串.
解决方法 使用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.
总结以上是内存溢出为你收集整理的ios – 在Swift中向UIButton添加字符串属性全部内容,希望文章能够帮你解决ios – 在Swift中向UIButton添加字符串属性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)