Ios 继续if语句中的计数器
我正在用iOS版的swift制作秒表应用程序。 制作应用程序,以秒为单位显示时间,这不是问题。现在我想更改它,使其以分钟和秒为单位显示时间,例如,1分20秒为1:20。 为了实现上述目标,我做了以下工作:Ios 继续if语句中的计数器,ios,if-statement,swift,counter,Ios,If Statement,Swift,Counter,我正在用iOS版的swift制作秒表应用程序。 制作应用程序,以秒为单位显示时间,这不是问题。现在我想更改它,使其以分钟和秒为单位显示时间,例如,1分20秒为1:20。 为了实现上述目标,我做了以下工作: import UIKit class ViewController: UIViewController { var timer = NSTimer() var seconds:Int = 0 @IBOutlet weak var timerOutput: UILabel! @
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var seconds:Int = 0
@IBOutlet weak var timerOutput: UILabel!
@IBAction func startButton(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
}
@IBAction func stopButton(sender: AnyObject) {
timer.invalidate()
}
@IBAction func resetButton(sender: AnyObject) {
timer.invalidate()
seconds = 0
timerOutput.text = "\(seconds)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
timerOutput.text = "0:\(seconds)"
}
func result() {
seconds++
if seconds <= 60 {
timerOutput.text = "0:\(seconds)"
} else {
if seconds > 60 {
var minutes:Int = seconds/60
var newSeconds = minutes % seconds
timerOutput.text = "\(minutes):\(newSeconds)"
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
导入UIKit
类ViewController:UIViewController{
var timer=NSTimer()
变量秒数:Int=0
@IBOutlet弱var时间路由输出:UILabel!
@iAction func开始按钮(发送方:AnyObject){
timer=NSTimer.scheduledTimerWithTimeInterval(1,目标:self,选择器:选择器(“结果”),userInfo:nil,repeats:true)
}
@iAction func stopButton(发件人:AnyObject){
timer.invalidate()
}
@iAction func resetButton(发件人:AnyObject){
timer.invalidate()
秒=0
timerOutput.text=“\(秒)”
}
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
timerOutput.text=“0:\(秒)”
}
func结果(){
秒++
如果60秒{
变量分钟数:整数=秒/60
var newSeconds=分钟%s
timerOutput.text=“\(分钟):\(新闻秒)”
}
}
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}
问题是,应用程序在第二个if语句之后不继续使用计数器,我定义了60秒后发生的事情。我已经尝试了很多方法,从使用for和/或while语句到创建一个全新的函数(秒数>60),但似乎没有任何效果。
目前,该应用程序运行到0:60,然后显示1:1,到目前为止还不错,然后什么都不做。然后每分钟更新一次,显示2:2、3:3、4:4等等
非常感谢您的帮助 newSeconds的数学应该是这样的:
// number of seconds in a minute, not the number of seconds currently
var newSeconds = seconds % 60
你说得对,我犯了个愚蠢的错误。但它不应该是:var newSeconds=seconds%60是的。我犯了一个愚蠢的错误:)是的,就是这样。它现在运行良好,我花了3个多小时在这样一个愚蠢的事情上。无论如何谢谢你!