I want to make Stop watch . On clicking on Start Button My timer is Start button on Click Stop my timer not stop. And on clicking Reset My timer reset then start again it is not right behaviour. Please help. I am newer in swift. Any help would be apperciated.Thamks in Advance
我想做秒表。点击开始按钮,我的定时器是启动按钮,点击停止我的计时器停止。点击重置我的计时器然后重新开始,这不是正确的行为。请帮助。我是斯威夫特新来的。任何帮助都将被告知。Thamks提前
class ViewController: UIViewController {
@IBOutlet var label:UILabel?
var time: Int!
var appTimer :NSTimer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
time = 0
label?.text = self.getFormattedString()
// Do any additional setup after loading the view.
}
}
extension ViewController
{
func getFormattedString() ->NSString
{
var t :Int = time!
var minQuotient :Int = time/600
var minRemainder:Int = time%600
var secQuotient:Int = minRemainder/10
var secRemainder:Int = minRemainder%10
var minString :NSString = NSString(format: "%d", minQuotient)
var secString :NSString = NSString(format: "%d", secQuotient)
if secQuotient < 10
{
secString = NSString(format: "0%@", secString)
}
var secRemainderString :NSString = NSString(format: "%d", secRemainder)
return NSString(format: "%@:%@.%@",minString,secString,secRemainderString)
}
@IBAction func statrt()
{
appTimer.invalidate()
var timer :NSTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("stopWatch:") , userInfo: nil, repeats: true)
}
func stopWatch (theTimer:NSTimer)
{
time = time+1
var b : String = self.getFormattedString()
label?.text = b
}
@IBAction func stop()
{
self.appTimer.invalidate()
}
@IBAction func reset()
{
self.appTimer.invalidate()
time = 0
label!.text = self.getFormattedString()
}
}
3 个解决方案
#1
1
Your timer instance variable should really be optional and not initialised to an invalid timer instance:
您的计时器实例变量应该是可选的,而不是初始化为无效的计时器实例:
var appTimer :NSTimer?
Then, when you create your new timer instance you need to store it into that instance variable instead of just creating it as a local variable in the function:
然后,当您创建新的计时器实例时,您需要将它存储到该实例变量中,而不是仅仅将其创建为函数中的局部变量:
appTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("stopWatch:") , userInfo: nil, repeats: true)
I would also recommend that you try to be consistent about using self.
so you know what you're doing in relation to instance and local state management.
我也会建议你在使用自我的时候保持一致。你知道你在做什么关于实例和地方*管理。
#2
1
You are confusing yourself between instance variables and plain method variables.
您正在混淆实例变量和普通方法变量。
You don't always want a timer, so appTimer should be optional and nil, where you pointlessly create a timer. stop method invalidates any existing timer and sets the optional to nil. start method calls stop, then creates a new scheduled timer. And your deinit method calls stop.
您并不总是需要计时器,所以appTimer应该是可选的,nil,您可以在这里毫无意义地创建一个计时器。停止方法使任何现有计时器无效,并将可选设置为nil。启动方法调用stop,然后创建一个新的计划计时器。你的deinit方法调用stop。
#3
0
func start() {
print("Fired")
self.myTimer.invalidate()
let ntimer :NSTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector:Selector("stopWatch") , userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(ntimer, forMode: NSDefaultRunLoopMode)
}
func stopWatch() {
print("stopped")
}
In console this was printing Fired stopped stopped stopped you should find a place in your code stop the second timer.
在控制台,这是打印停止停止停止你应该在你的代码中找到一个地方停止第二个计时器。
#1
1
Your timer instance variable should really be optional and not initialised to an invalid timer instance:
您的计时器实例变量应该是可选的,而不是初始化为无效的计时器实例:
var appTimer :NSTimer?
Then, when you create your new timer instance you need to store it into that instance variable instead of just creating it as a local variable in the function:
然后,当您创建新的计时器实例时,您需要将它存储到该实例变量中,而不是仅仅将其创建为函数中的局部变量:
appTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("stopWatch:") , userInfo: nil, repeats: true)
I would also recommend that you try to be consistent about using self.
so you know what you're doing in relation to instance and local state management.
我也会建议你在使用自我的时候保持一致。你知道你在做什么关于实例和地方*管理。
#2
1
You are confusing yourself between instance variables and plain method variables.
您正在混淆实例变量和普通方法变量。
You don't always want a timer, so appTimer should be optional and nil, where you pointlessly create a timer. stop method invalidates any existing timer and sets the optional to nil. start method calls stop, then creates a new scheduled timer. And your deinit method calls stop.
您并不总是需要计时器,所以appTimer应该是可选的,nil,您可以在这里毫无意义地创建一个计时器。停止方法使任何现有计时器无效,并将可选设置为nil。启动方法调用stop,然后创建一个新的计划计时器。你的deinit方法调用stop。
#3
0
func start() {
print("Fired")
self.myTimer.invalidate()
let ntimer :NSTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector:Selector("stopWatch") , userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(ntimer, forMode: NSDefaultRunLoopMode)
}
func stopWatch() {
print("stopped")
}
In console this was printing Fired stopped stopped stopped you should find a place in your code stop the second timer.
在控制台,这是打印停止停止停止你应该在你的代码中找到一个地方停止第二个计时器。