麻烦调试 NSTimer

Trouble Debugging NSTimer

我正在尝试让图像显示 1 秒并在 1 秒结束后消失。当我构建我的应用程序时它工作正常但是当我 运行 我的应用程序崩溃时。这是我的代码:

var flashTimer = NSTimer()
var timerCounter = 0
var currentTime = 0
var randomImageGeneratorNumber = 0
var flashingImageView = UIImageView()
var flashButton = UIButton()

@IBAction func flashButton(sender: UIButton) {

    var randomImageGeneratorNumber = arc4random_uniform(6) + 1
    if flashButton.hidden {
        flashButton.hidden = true
        flashingImageView.hidden = false
    }
    if randomImageGeneratorNumber == 1 {
        flashingImageView.image = UIImage(named: "Image1.png")
    }
    if randomImageGeneratorNumber == 2 {
        flashingImageView.image = UIImage(named: "Image2.png")
    }
    if randomImageGeneratorNumber == 3 {
        flashingImageView.image = UIImage(named: "Image3.png")
    }
    if randomImageGeneratorNumber == 4 {
        flashingImageView.image = UIImage(named: "Image4.png")
    }
    if randomImageGeneratorNumber == 5 {
        flashingImageView.image = UIImage(named: "Image5.png")
    }
    if randomImageGeneratorNumber == 6 {
        flashingImageView.image = UIImage(named: "Image6.png")
    }
    var currentTime = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timerFunc", userInfo: nil, repeats: false)
    func timerFunc(sender: AnyObject){
        flashingImageView.hidden = true;
    }
}

调试器输出中的错误是: [Flash_Facts.ViewControllertimerFunc]:无法识别的选择器发送到实例 0x7f9ceb774810

我不确定如何解决这个问题。有什么建议吗?

提前致谢。

问题是您必须在 IBAction 函数之外创建您的函数。这同样适用于您的计时器 var ( currentTime )

var currentTime = NSTimer()
func timerFunc() {
     flashButton.hidden = !flashButton.hidden
     flashingImageView.hidden = !flashingImageView.hidden
}
@IBAction func flashButton(sender: UIButton) {
     flashButton.hidden = !flashButton.hidden
     flashingImageView.hidden = !flashingImageView.hidden
     flashingImageView.image = UIImage(named: "Image\(arc4random_uniform(6) + 1).png")
     currentTime = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timerFunc", userInfo: nil, repeats: false)

}