Come è possibile associare una proprietà stringa a UIButton
in Swift? Non voglio che la stringa appaia come testo del pulsante, semplicemente per essere assegnata al pulsante come identificatore o chiave. Ecco quello che ho finora:Aggiungere una proprietà stringa a un UIButton in Swift
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)
}
Quindi penso ho bisogno di qualcosa dopo
// Set the identifier for each answer
self.identifier = self.currentQuestion!.answerIdentifier[index]
Per assegnare l'identificatore al pulsante.
La ragione di ciò è che sto tentando di implementare una logica dell'albero decisionale in modo da poter tenere traccia di ogni pulsante di risposta che viene toccato per generare una stringa di codice che corrisponderà a un risultato finale.
perché non usi la proprietà del tag? –
@LeonardoSavioDabus Tag non è una stringa. È un 'NSInteger'. – nhgrif
@nhgrif So che se non sta visualizzando il testo non vedo la necessità di una stringa –