2016-02-22 57 views
0

我想在一段時間後使用計時器在標籤內顯示不同的文本,但不能增加我的自定義類型數組的元素。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。

+0

只儲存當前鍛鍊的指數,該指數從數組,而不是硬編碼的數字 –

回答

0

試試這個,它使用了兩種方法和一個索引。

工作流

  • 最初index被設置爲0和counter被設置爲15。
  • updateExercise()被調用。

    • 練習用戶界面已更新。
    • 如果index爲0,則創建timer
  • timerAction()在定時器啓動後被調用。

    • 計數器遞減並且計數器標籤被更新。
    • 如果counter爲0且index等於練習-1的數量,則計時器失效。
    • 如果counter是0和index <號演習-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() 
     } 
     } 
    } 
+0

完美的作品檢索的'Exercise'對象時,你會增加和使用,謝謝! –

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 
     } 
    } 
} 

不要重複自己。某項任務只能由一個函數完成,這個特定任務的代碼不能在別處。

相關問題