繁体   English   中英

Swift:如何增加数组中的元素

[英]Swift: How to increment an element in an array

我试图使用计时器在一段时间后在标签内显示不同的文本,但是无法增加自定义类型数组的元素。

这是代码:

    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等重复此方法似乎很笨拙且不必要,因此需要一种增加数组值的方法,而不必在不同的函数中分别调用。

我没有对此进行测试,但是应该可以。

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

不要重复自己。 一项特定任务只能由一个功能完成,该特定任务的代码不得在其他位置。

试试这个,它使用两种方法和一个索引。

工作流程

  • 最初, index设置为0, counter设置为15。
  • 将调用updateExercise()

    • 练习UI已更新。
    • 如果index为0,则创建timer
  • 计时器触发后调用timerAction()

    • 计数器递减并更新计数器标签。
    • 如果counter为0并且index等于练习次数-1,则计时器无效。
    • 如果counter为0,并且index <练习次数-1 index递增, updateExercise() 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()
        }
      }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM