我试图将图像显示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.ViewController timerFunc]:无法识别的选择器发送到实例0x7f9ceb774810
我不知道如何解决这个问题。有什么建议吗?
提前致谢。
答案 0 :(得分:1)
问题是你必须在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)
}