我知道这是很多要求,但我已经尝试了很多其他的解决方案,我似乎不能正确的。
所以,我有一个类来计数,在倒计时结束时,一个新的视图开始了。
这是倒计时班:
import Foundation
import UIKit
class CountdownController: UIViewController {
// MARK: Properties
@IBOutlet weak var countDownLabel: UILabel!
var count = 3
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
func update() {
if(count > 0) {
countDownLabel.text = String(count--)
}
else {
self.performSegueWithIdentifier("goestoMathTest", sender: self)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Actions
}在MathTestController显示之后出现的错误是:
2016-05-26 23:43:48.579 TraderMathTestiOS[18654:951105] Warning: Attempt to
present <TraderMathTestiOS.MathTestController: 0x7fca824be7d0> on
<TraderMathTestiOS.CountdownController: 0x7fca824b9d70> whose view is not in
the window hierarchy!**编辑:所以我尝试了另一个改变一些事情,我想我缩小了问题。我将计时器更改为viewDidLoad()中的计时器,并将重复更改为'false‘,现在MathTestController在1秒后出现,没有出现警告。以下是修改后的代码:
override func viewDidLoad() {
super.viewDidLoad()
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: false)
}
func update() {
self.performSegueWithIdentifier("goestoMathTest", sender: self)
if(count > 0) {
countDownLabel.text = String(count--)
}
else {
self.performSegueWithIdentifier("goestoMathTest", sender: self)
}
}我认为出现错误的原因是,即使在调用CountdownController之后,计时器仍会在MathTestController中重复。有人知道如何在没有错误的情况下获得我最初的功能(计数'3,2,1‘的计时器)?也许我需要用某种方式杀死计时器?
发布于 2016-05-27 05:54:08
我终于修好了这个。对于任何想知道,如果您的计时器重复,您需要使它无效,如果您要启动一个新的视图。这是固定代码:
class CountdownController: UIViewController {
// MARK: Properties
@IBOutlet weak var countDownLabel: UILabel!
var timer = NSTimer()
var count = 3
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
func update() {
if(count > 0) {
countDownLabel.text = String(count--)
}
else {
timer.invalidate()
self.performSegueWithIdentifier("goestoMathTest", sender: self)
}
}https://stackoverflow.com/questions/37474681
复制相似问题