简体   繁体   中英

Swift Run An NSTimer Automatically

I'm making an app and using NSTimer to make the timer in my app.But I need the NSTimer to run when the scene start.I have two scene in my app a homescreen and the app it's self here is my code viewcontroller.swift (The second scene the first scene in empty) And btw it's my 3 day using swift and i'm in middle school ;).

 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.
}
*/

}

If you want to start your timer when the scene loads call DisplayTimer() under override func viewDidLoad() when the view is loaded

Also if you want to update your label every second, the selector in your NSTimer is calling updateCounter not updateTimer

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)" 

    ... 
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM