Swift自动运行NSTimer

时间:2016-05-20 23:12:07

标签: ios swift nstimer

我正在制作一个应用程序,并使用NSTimer在我的应用程序中制作计时器。但我需要NSTimer在场景开始时运行。我的应用程序中有两个场景主屏幕和应用程序它自己在这里是我的代码viewcontroller.swift(第二个场景是空的第一个场景)顺便说一句,这是我使用swift的3天,我在中学;)。

 import UIKit
 var Number = 2
 var Answer = Number * 2
 var score = 0
var scorelabel = "Score: "
var Timer = NSTimer()
var Counter = 10

class SecondViewController: UIViewController {


@IBOutlet weak var RightAndWrongLabel: UILabel!

@IBOutlet weak var TimerLabel: UILabel!
@IBOutlet weak var RightAndWrong: UIImageView!
@IBOutlet weak var ScoreIabel: UILabel!
@IBOutlet weak var UserInputAnswer: UITextField!
@IBOutlet weak var Question: UILabel!
@IBOutlet weak var TimerOut: UIImageView!

var seconds = 0
var timeison = true







@IBAction func ConfirmAnswer(sender: AnyObject) {
    let UserAnswer = Int(UserInputAnswer.text!)
    if UserAnswer == Answer {
        print("Your right")
        Number += 2
        score += 1
        ScoreIabel.text = "Score: \(score)"
        Question.text = "\(Number) x 2"
        UserInputAnswer.text = ""
        Answer = Number * 2
        RightAndWrong.image = UIImage(named: "Label")
        RightAndWrongLabel.hidden = false
        RightAndWrongLabel.text = "Right!"
        RightAndWrongLabel.textColor  = UIColor(red: 0, green: 225, blue: 0, alpha: 1)

    } else {
        UserInputAnswer.text  = ""
        RightAndWrong.image = UIImage(named: "Label")
        RightAndWrongLabel.hidden = false
        RightAndWrongLabel.text = "Wrong!"
        RightAndWrongLabel.textColor = UIColor(red: 225, green: 0, blue: 0, alpha: 1)



    }



}

func DisplayTimer() {
    Timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}


func updateTimer(){
    TimerLabel.text = String(Counter--)
}
override func viewDidLoad() {
    super.viewDidLoad()
    UserInputAnswer.keyboardType = UIKeyboardType.NumberPad
    RightAndWrongLabel.hidden = true

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    3
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

2 个答案:

答案 0 :(得分:0)

如果要在场景加载时启动计时器,请调用DisplayTimer() 加载视图时override func viewDidLoad()

此外,如果您想每秒更新一次标签,NSTimer中的选择器正在调用updateCounter而不是updateTimer

答案 1 :(得分:0)

Swift 2.x:

func DisplayTimer() {
    Timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target:self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}

func updateTimer() {
    if Counter != 0 {  TimerLabel.text = "\(Counter -= 1)" 
    } else { 
        Timer.invalidate() 
        // call a game over method here... 
    }
 }

override func viewDidLoad() {
    super.viewDidLoad()
   ... 

     // start the timer when this controller shows up
     DisplayTimer()
     TimerLabel.text = "\(Counter)" 

    ... 
   }