xcode 7个带限制的定时器
xcode 7 Timer with Limit
我已经制作了一个带有计时器的应用程序,但现在我希望计时器在一段时间后停止,例如 10 秒后。当计时器结束时,它会转到下一个视图控制器。如果有人能帮助我,我会很高兴。先感谢您。这是我已有的代码:
导入 UIKit
class Level1: UIViewController {
var timerCount = 0
var timmerRunning = false
var timer = NSTimer()
@IBOutlet weak var timerLabel: UILabel!
func Counting() {
timerCount += 1
timerLabel.text = "\(timerCount)"
}
@IBAction func startButton(sender: UIButton) {
if timmerRunning == false{
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)
self.timmerRunning = true
}
}
@IBAction func stopButton(sender: UIButton) {
if timmerRunning == true{
self.timer.invalidate()
self.timmerRunning = false
}
}
@IBAction func restartButton(sender: UIButton) {
timerCount = 0
timerLabel.text = "0"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
试试这个:
func Counting() {
timerCount += 1
timerLabel.text = "\(timerCount)"
if timerCount == 10 {
self.timer.invalidate()
//go to the next view controller, you'll need to create a navigation controller if you don't have one
navigaionController.pushViewController(someViewController, animated:false)
}
我已经制作了一个带有计时器的应用程序,但现在我希望计时器在一段时间后停止,例如 10 秒后。当计时器结束时,它会转到下一个视图控制器。如果有人能帮助我,我会很高兴。先感谢您。这是我已有的代码:
导入 UIKit
class Level1: UIViewController {
var timerCount = 0
var timmerRunning = false
var timer = NSTimer()
@IBOutlet weak var timerLabel: UILabel!
func Counting() {
timerCount += 1
timerLabel.text = "\(timerCount)"
}
@IBAction func startButton(sender: UIButton) {
if timmerRunning == false{
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counting"), userInfo: nil, repeats: true)
self.timmerRunning = true
}
}
@IBAction func stopButton(sender: UIButton) {
if timmerRunning == true{
self.timer.invalidate()
self.timmerRunning = false
}
}
@IBAction func restartButton(sender: UIButton) {
timerCount = 0
timerLabel.text = "0"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
试试这个:
func Counting() {
timerCount += 1
timerLabel.text = "\(timerCount)"
if timerCount == 10 {
self.timer.invalidate()
//go to the next view controller, you'll need to create a navigation controller if you don't have one
navigaionController.pushViewController(someViewController, animated:false)
}