我想在一段時間後使用計時器在標籤內顯示不同的文本,但不能增加我的自定義類型數組的元素。Swift:如何增加數組中的元素
下面是代碼:
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等似乎clumbsy和不必要的,所以想的方式遞增數組值,而不必在不同的功能調用seperately。
只儲存當前鍛鍊的指數,該指數從數組,而不是硬編碼的數字 –