计数器不会在按下按钮时减去?
Counter wont subtract on button press?
我试图在每次调用 endSetPressed 操作时将我的 setCount 设置为 -1。
我已将 setCount -= 1 包含到 updateTimer 函数中,以便在按下 endSetPressed 时,它应该从 setCount 中取 -1,然后启动 restCount 计时器。然而在实践中它似乎并没有显示它在应用程序中执行 -1 它只是保持固定在起始 setCount 值。
另一个问题是我希望能够在用户设置 restStepperValueChanged 值时看到 restRemainingCountdownLabel 的值,我认为这是通过 'restRemainingCountdownLabel.text = String(restCount)' 完成的,但是我使用 'restCount = Int(sender.value)*60' 来以分钟为单位生成一个值,它以秒而不是分钟显示 restRemainingCountdownLabel,也感谢一些指导!
这是我的代码:
@IBOutlet weak var restRemainingCountdownLabel: UILabel!
@IBOutlet weak var setsRemainingCountdownLabel: UILabel!
@IBOutlet weak var numberOfSetsLabel: UILabel!
@IBOutlet weak var numberOfRestLabel: UILabel!
@IBOutlet weak var adjustSetsStepper: UIStepper!
@IBOutlet weak var adjustRestStepper: UIStepper!
var restTimer: Timer?
var restCount = 0
var setCount = 0
@IBAction func endSetPressed(_ sender: Any) {
restTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RestController.updateTimer), userInfo: nil, repeats: true)
}
@IBAction func setStepperValueChanged(_ sender: UIStepper) {
numberOfSetsLabel.text = Int(sender.value).description
setCount = Int(sender.value)
setsRemainingCountdownLabel.text = String(setCount)
}
@IBAction func restStepperValueChanged(_ sender: UIStepper) {
numberOfRestLabel.text = Int(sender.value).description
restCount = Int(sender.value)*60
restRemainingCountdownLabel.text = String(restCount)
}
@IBAction func resetSetsButton(_ sender: Any) {
//setCount = Int
}
override func viewDidLoad() {
super.viewDidLoad()
}
func updateTimer() {
if (setCount > 0){
setCount -= 1
}
if (restCount > 0){
let minutes = String(restCount / 60)
let seconds = String(restCount % 60)
restRemainingCountdownLabel.text = minutes + ":" + seconds
restCount -= 1
}
}
看看你的 updateTimer()
方法,第一行:
if (setCount > 0)
if setCount > 0
则 setCount
减 1
但是...您在这里声明了 setCount
的值为 0
var setCount = 0
所以,你永远不会在你的 if
部分结束:)
如何调试
下次遇到此类问题时,请尝试在代码中添加一些 print()
语句。
在你的情况下你可以这样做:
@IBAction func endSetPressed(_ sender: Any) {
print("endSetPressed")
restTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RestController.updateTimer), userInfo: nil, repeats: true)
}
@IBAction func setStepperValueChanged(_ sender: UIStepper) {
numberOfSetsLabel.text = Int(sender.value).description
setCount = Int(sender.value)
setsRemainingCountdownLabel.text = String(setCount)
}
@IBAction func restStepperValueChanged(_ sender: UIStepper) {
numberOfRestLabel.text = Int(sender.value).description
restCount = Int(sender.value)*60
restRemainingCountdownLabel.text = String(restCount)
}
@IBAction func resetSetsButton(_ sender: Any) {
//setCount = Int
}
override func viewDidLoad() {
super.viewDidLoad()
}
func updateTimer() {
print("updateTimer")
if (setCount > 0){
print("setCount > 0")
setCount -= 1
print("setCount is now \(setCount)")
}
if (restCount > 0){
print("restCount > 0")
let minutes = String(restCount / 60)
let seconds = String(restCount % 60)
restRemainingCountdownLabel.text = minutes + ":" + seconds
restCount -= 1
print("restCount is now \(restCount) - minutes: \(minutes) - seconds: \(seconds)")
}
}
这应该会给你一些关于正在发生的事情的指示,"path" 你的代码将遵循。
希望对你有所帮助。
我试图在每次调用 endSetPressed 操作时将我的 setCount 设置为 -1。
我已将 setCount -= 1 包含到 updateTimer 函数中,以便在按下 endSetPressed 时,它应该从 setCount 中取 -1,然后启动 restCount 计时器。然而在实践中它似乎并没有显示它在应用程序中执行 -1 它只是保持固定在起始 setCount 值。
另一个问题是我希望能够在用户设置 restStepperValueChanged 值时看到 restRemainingCountdownLabel 的值,我认为这是通过 'restRemainingCountdownLabel.text = String(restCount)' 完成的,但是我使用 'restCount = Int(sender.value)*60' 来以分钟为单位生成一个值,它以秒而不是分钟显示 restRemainingCountdownLabel,也感谢一些指导!
这是我的代码:
@IBOutlet weak var restRemainingCountdownLabel: UILabel!
@IBOutlet weak var setsRemainingCountdownLabel: UILabel!
@IBOutlet weak var numberOfSetsLabel: UILabel!
@IBOutlet weak var numberOfRestLabel: UILabel!
@IBOutlet weak var adjustSetsStepper: UIStepper!
@IBOutlet weak var adjustRestStepper: UIStepper!
var restTimer: Timer?
var restCount = 0
var setCount = 0
@IBAction func endSetPressed(_ sender: Any) {
restTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RestController.updateTimer), userInfo: nil, repeats: true)
}
@IBAction func setStepperValueChanged(_ sender: UIStepper) {
numberOfSetsLabel.text = Int(sender.value).description
setCount = Int(sender.value)
setsRemainingCountdownLabel.text = String(setCount)
}
@IBAction func restStepperValueChanged(_ sender: UIStepper) {
numberOfRestLabel.text = Int(sender.value).description
restCount = Int(sender.value)*60
restRemainingCountdownLabel.text = String(restCount)
}
@IBAction func resetSetsButton(_ sender: Any) {
//setCount = Int
}
override func viewDidLoad() {
super.viewDidLoad()
}
func updateTimer() {
if (setCount > 0){
setCount -= 1
}
if (restCount > 0){
let minutes = String(restCount / 60)
let seconds = String(restCount % 60)
restRemainingCountdownLabel.text = minutes + ":" + seconds
restCount -= 1
}
}
看看你的 updateTimer()
方法,第一行:
if (setCount > 0)
if setCount > 0
则 setCount
但是...您在这里声明了 setCount
的值为 0
var setCount = 0
所以,你永远不会在你的 if
部分结束:)
如何调试
下次遇到此类问题时,请尝试在代码中添加一些 print()
语句。
在你的情况下你可以这样做:
@IBAction func endSetPressed(_ sender: Any) {
print("endSetPressed")
restTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RestController.updateTimer), userInfo: nil, repeats: true)
}
@IBAction func setStepperValueChanged(_ sender: UIStepper) {
numberOfSetsLabel.text = Int(sender.value).description
setCount = Int(sender.value)
setsRemainingCountdownLabel.text = String(setCount)
}
@IBAction func restStepperValueChanged(_ sender: UIStepper) {
numberOfRestLabel.text = Int(sender.value).description
restCount = Int(sender.value)*60
restRemainingCountdownLabel.text = String(restCount)
}
@IBAction func resetSetsButton(_ sender: Any) {
//setCount = Int
}
override func viewDidLoad() {
super.viewDidLoad()
}
func updateTimer() {
print("updateTimer")
if (setCount > 0){
print("setCount > 0")
setCount -= 1
print("setCount is now \(setCount)")
}
if (restCount > 0){
print("restCount > 0")
let minutes = String(restCount / 60)
let seconds = String(restCount % 60)
restRemainingCountdownLabel.text = minutes + ":" + seconds
restCount -= 1
print("restCount is now \(restCount) - minutes: \(minutes) - seconds: \(seconds)")
}
}
这应该会给你一些关于正在发生的事情的指示,"path" 你的代码将遵循。
希望对你有所帮助。