我尝试使用计时器在一段时间后在标签中显示不同的文本,但是无法增加我的自定义类型数组的元素。
以下是代码:
import UIKit
class WorkWorkoutViewOne: UIViewController {
@IBOutlet weak var exerciseTitle: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var instructionsLabel: UILabel!
@IBOutlet weak var exerciseImage: UIImageView!
var counter = 15
var timer: NSTimer?
var workoutExercisesShuffled = [Exercise]()
override func viewDidLoad() {
super.viewDidLoad()
let image: UIImage = workoutExercisesShuffled[0].filename!
exerciseImage.image = image
let titleLabel = workoutExercisesShuffled[0].name
exerciseTitle.text = titleLabel
let instructionsTitle = workoutExercisesShuffled[0].instructions
instructionsLabel.text = instructionsTitle
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerAction", userInfo: nil, repeats: true)
}
var timerTwo: NSTimer?
var counterTwo = 15
func timerAction() {
--counter
timerLabel.text = "\(counter)"
if (counter == 0) {
timer?.invalidate()
let image: UIImage = workoutExercisesShuffled[1].filename!
exerciseImage.image = image
let titleLabel = workoutExercisesShuffled[1].name
exerciseTitle.text = titleLabel
let instructionsTitle = workoutExercisesShuffled[1].instructions
instructionsLabel.text = instructionsTitle
timerLabel.text = "\(counterTwo)"
var timerTwo = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerActionTwo", userInfo: nil, repeats: true)
}
}
使用timerThree,timerFour等重复这个方法似乎很笨拙和不必要,所以想要一种增加数组值的方法,而不是单独调用不同的函数。
答案 0 :(得分:0)
我没有对此进行测试,但它应该有效。
import UIKit
class WorkWorkoutViewOne: UIViewController {
@IBOutlet weak var exerciseTitle: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var instructionsLabel: UILabel!
@IBOutlet weak var exerciseImage: UIImageView!
var count = 15
var index = 0
var timer: NSTimer
var countdown: NSTimer
var workoutExercisesShuffled = [Exercise]()
override func viewDidLoad() {
super.viewDidLoad()
// TODO: load your exercises
var timer = NSTimer.scheduledTimerWithTimeInterval(15, target: self, selector: "displayNewExercise", userInfo: nil, repeats: true)
}
func displayNewExercise() {
countdown = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "displayCountdown", userInfo: nil, repeats: true)
if (index < workoutExercisesShuffled.count) {
exerciseImage.image = workoutExercisesShuffled[index].filename!
exerciseTitle.text = workoutExercisesShuffled[index].name
instructionsLabel.text = workoutExercisesShuffled[index].instructions
++index
}
else {
timer.invalidate()
countdown.invalidate()
}
}
func displayCountdown() {
if(count > 0) {
countDownLabel.text = String(--count)
}
else {
countdown.invalidate()
count = 15
}
}
}
不要重复自己。某个任务只能由一个函数完成,这个特定任务的代码不能在其他地方。
答案 1 :(得分:0)
试试这个,它使用两种方法和一个索引。
<强>工作流强>
index
设置为0,counter
设置为15. updateExercise()
被调用。
index
为0,则会创建timer
。 timerAction()
。
counter
为0且index
等于练习次数-1,则计时器无效。counter
为0且index
&lt;练习次数-1 index
递增,counter
重置为15,再次调用updateExercise()
。 var counter = 15
var index = 0
var timer: NSTimer?
var workoutExercisesShuffled = [Exercise]()
override func viewDidLoad() {
super.viewDidLoad()
updateExercise()
}
func updateExercise() {
let image: UIImage = workoutExercisesShuffled[index].filename!
exerciseImage.image = image
let titleLabel = workoutExercisesShuffled[index].name
exerciseTitle.text = titleLabel
let instructionsTitle = workoutExercisesShuffled[index].instructions
instructionsLabel.text = instructionsTitle
if index == 0 {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerAction", userInfo: nil, repeats: true)
}
}
func timerAction() {
counter -= 1
timerLabel.text = "\(counter)"
if (counter == 0) {
if index == workoutExercisesShuffled.count - 1 {
timer!.invalidate()
timer = nil
} else {
index += 1
counter = 15
updateExercise()
}
}
}