dimanche 22 mars 2015

Index of UIButton in Collection using Swift


I have Swift code that looks like what's below. I have a collection of four buttons that I've put into an IBOutlet Collection. Those four buttons also link to a @IBAction. When I press a button I am able to see the title of the button that was pressed, but I also want to know the index for that button that was press, so that I can disable that particular button.


How can I find the index of the button that was pressed.


Thanks.




import UIKit


class ViewController: UIViewController {



@IBOutlet var answerButtons: [UIButton]!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


@IBAction func pressedButton(sender: UIButton) {
let buttonTitle = sender.titleForState(UIControlState.Normal)
println("Pressed Button \(buttonTitle)")
listbuttons()
}

func listbuttons() {
for button in answerButtons {
let buttonTitle = button.titleForState(UIControlState.Normal)

println("Button Title: \(buttonTitle) at Index: \(button)")
}
}




Aucun commentaire:

Enregistrer un commentaire