I have a problem with the sixth chapter of the basic Swift tutorial from Apple.
The problem is that only the first star (UIButton) is working. I created all the stars in a for loop and call for every UIButton the addTarget method once. So basically the code is the same for every button. And now a very strange behavior is coming up, on the iPad simulators everything is working fine in diffece to this on all iPhones Simulators < iPhone 6 no star works and on all other iPhones only the first star is working, same on my real iPhone 6.
init method with for loop:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let filledStarImage = UIImage(named: "filledStar")
let emptyStarImage = UIImage(named: "emptyStar")
for _ in 0..<starCount {
let button = UIButton()
button.setImage(emptyStarImage, forState: .Normal)
button.setImage(filledStarImage, forState: .Selected)
button.setImage(filledStarImage, forState: [.Highlighted, .Selected])
button.adjustsImageWhenHighlighted = false
ratingButtons += [button]
ratingButtons.last!.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)
self.addSubview(button)
}
}
action method:
func ratingButtonTapped(button: UIButton){
rating = ratingButtons.indexOf(button)! + 1
updateButtonSelectionState()
}
func updateButtonSelectionState() {
for (index, button) in ratingButtons.enumerate(){
button.selected = index < rating
}
}
Do you have any suggestions how to make the buttons working on every device?
