Cate Cate - 3 months ago 9
Swift Question

Swift NSTimer Coding

Using Swift 2.0 and trying to implement Swift NSTimer in Custom Keyboard. Button2 shows up great when Button1 is pressed, until the NSTimer kicks in and crashes the keyboard.

I'm trying to hide Button2 after a few seconds.

@IBAction func Button1(sender: UIButton) {
Button2.setTitle("Text", forState: UIControlState.Normal)

NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("Hide:"), userInfo: nil, repeats: false)
Button2.hidden = false


This should work (note that I changed some of the function names to more closely follow conventions):

@IBAction func button1Tapped() {
    button2.setTitle("Text", forState: .Normal)
    button2.hidden = false
    NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "hideButton2", userInfo: nil, repeats: false)

@objc func hideButton2() {
    button2.hidden = true