1

So I am trying to create 10 buttons by using a for in loop and make all those 10 buttons move down using a CADisplayLink. The problem is that my CADisplayLink only moves one of the buttons down and I want it to move all 10 of the buttons. Please Help! Thanks in advance!

var button: UIButton!



override func viewDidLoad() {
    super.viewDidLoad()

    var displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
    displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

    for index in 0...10 {

        var xLocation:CGFloat = CGFloat(arc4random_uniform(300) + 30)

        button = UIButton.buttonWithType(UIButtonType.System) as UIButton

        button.frame = CGRectMake(xLocation, 10, 100, 100)
        button.setTitle("Test Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(button)

        }



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func handleDisplayLink(displayLink: CADisplayLink) {

    for index in 0...10 {

        var buttonFrame = button.frame
        buttonFrame.origin.y += 1
        button.frame = buttonFrame
        if button.frame.origin.y >= 500 {
            displayLink.invalidate()
        }
    }
}


func buttonAction(sender: UIButton) {
    sender.alpha = 0
}

}

Garret Kaye
  • 2,412
  • 4
  • 21
  • 45

1 Answers1

0

You've only got a reference to one of the 10 buttons you've created in viewDidLoad. Use an array of type button [UIButton] to store all 10 and then loop through each of them during your CADisplayLink callback.

Your declaration would be:

    var buttons: [UIButton] = Array(count: 10, repeatedValue: UIButton.buttonWithType(.System) as! UIButton)

And any time you referenced a button in your original code, reference the button at the current index of your for loop using the array index operator:

buttons[index]

An overview of Swift arrays and the standard library reference are here:

So the provided code would be:

var buttons: [UIButton] = Array(count: 10, repeatedValue: UIButton.buttonWithType(.System) as! UIButton)

override func viewDidLoad() {
    super.viewDidLoad()

    var displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
    displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

    for index in 0...10 {

        var xLocation:CGFloat = CGFloat(arc4random_uniform(300) + 30)

        buttons[index].frame = CGRectMake(xLocation, 10, 100, 100)
        buttons[index].setTitle("Test Button \(index)", forState: UIControlState.Normal)
        buttons[index].addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(buttons[index])

        }



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func handleDisplayLink(displayLink: CADisplayLink) {

    for index in 0...10 {

        var buttonFrame = buttons[index].frame
        buttonFrame.origin.y += 1
        buttons[index].frame = buttonFrame
        if buttons[index].frame.origin.y >= 500 {
            displayLink.invalidate()
        }
    }
}


func buttonAction(sender: UIButton) {
    sender.alpha = 0
}
Ralfonso
  • 1,614
  • 15
  • 30
  • How would I write that? – Garret Kaye Mar 02 '15 at 17:59
  • Do you have it working? If so, please mark the answer as correct, and if not, I'd be glad to offer further assistance. – Ralfonso Mar 02 '15 at 18:10
  • I cant seem to get it, can you show me what the whole thing would look like all together? – Garret Kaye Mar 02 '15 at 20:28
  • Updated the resulting code and provided links to documentation – Ralfonso Mar 02 '15 at 20:43
  • It crashes at buttons[index] = UIButton.buttonWithType(UIButtonType.System) as UIButton – Garret Kaye Mar 02 '15 at 20:53
  • Sorry about that - forgot to initialize the array. Another way to do it would be to initialize an empty array, then with each iteration of the loop, create a UIButton and configure it as you like, then append it to the buttons array with buttons.append(button). – Ralfonso Mar 02 '15 at 21:09
  • Im still so confused, I dont know how to make an arrray of buttons or anything – Garret Kaye Mar 03 '15 at 01:00
  • I've updated the code to show how to declare and initialize the array of 10 buttons. Have a look at the two links to the Swift language documentation and play around with some of the examples in a Playground to get a feel for how to work with arrays and the other collection types. Arrays are a fundamental programming concept and having a good understanding of them will only benefit your development as a programmer. – Ralfonso Mar 03 '15 at 17:47