如何检查现有数组Swift 3.0的按下按钮的顺序

时间:2017-07-03 18:07:05

标签: arrays swift xcode button

我想检查按下按钮的顺序(它们都有标签)对现有数组。例如,我有一个1-9和9按钮的数组,我想知道用户是否按正确的顺序按它们。如果没有,那么我将返回错误。看起来很简单,但我还在学习并耗尽了我的资源。在此先感谢!我已经设法取得进展,并使按钮在控制台上打印他们的标签,但似乎发生了一个小错误,我似乎无法将它们全部放在一个阵列中,它们都是单独打印的。

@IBAction func onePressed(_ sender: Any) {
        print("cell 1 was pressed while it showed \(shuffledArray[0])")
    }

现在已经解决了!感谢您的帮助!这是工作代码!

 @IBAction func pressed(_ sender: UIButton) {
    //print("cell  was pressed while it showed \(shuffledArray[0])")

    switch(sender.tag){

    case 1:
        order.add(sender.tag)//adding to nsmutable array use add or at a specific index use insert
        print(order)
        self.oneLabel.backgroundColor = UIColor.white

    case 2:
        order.add(sender.tag)
        print(order)
        self.twoLabel.backgroundColor = UIColor.white

    case 3:
        order.add(sender.tag)
        print(order)
        self.threeLabel.backgroundColor = UIColor.white

    case 4:
        order.add(sender.tag)
        print(order)
        self.fourLabel.backgroundColor = UIColor.white

    case 5:
        order.add(sender.tag)
        print(order)
        self.fiveLabel.backgroundColor = UIColor.white

    case 6:
        order.add(sender.tag)
        print(order)
        self.sixLabel.backgroundColor = UIColor.white

    case 7:
        order.add(sender.tag)
        print(order)
        self.sevenLabel.backgroundColor = UIColor.white

    case 8:
        order.add(sender.tag)
        print(order)
        self.eightLabel.backgroundColor = UIColor.white

    case 9:
        order.add(sender.tag)
        print(order)
        self.nineLabel.backgroundColor = UIColor.white

    default :
        break
    }

    if order.count == 9 {
        print(order.count)
        let indexOfShuffled = [1,2,3,4,5,6,7,8,9]
        let combined = zip(shuffledArray, indexOfShuffled).sorted {$0.0 < $1.0}
        print(combined)

        // use map to extract the individual arrays
        let sortedIndexOfShuffled = combined.map {$0.1}
        let sortedShuffledArray = combined.map {$0.0}

        print(sortedIndexOfShuffled)  // "[1.2, 1.5, 2.4, 10.9, 20.0]"
        print(sortedShuffledArray)  // "[1, 3, 0, 0, 2]"

        if order == sortedIndexOfShuffled as! NSMutableArray{
            self.oneLabel.setImage(UIImage(named: "tick.png"), for: .normal)
            self.twoLabel.setImage(UIImage(named: "tick.png"), for: .normal)
            self.threeLabel.setImage(UIImage(named: "tick.png"), for: .normal)
            self.fourLabel.setImage(UIImage(named: "tick.png"), for: .normal)
            self.fiveLabel.setImage(UIImage(named: "tick.png"), for: .normal)
            self.sixLabel.setImage(UIImage(named: "tick.png"), for: .normal)
            self.sevenLabel.setImage(UIImage(named: "tick.png"), for: .normal)
            self.eightLabel.setImage(UIImage(named: "tick.png"), for: .normal)
            self.nineLabel.setImage(UIImage(named: "tick.png"), for: .normal)
        }else{
            self.oneLabel.setImage(UIImage(named: "cross.png"), for: .normal)
            self.twoLabel.setImage(UIImage(named: "cross.png"), for: .normal)
            self.threeLabel.setImage(UIImage(named: "cross.png"), for: .normal)
            self.fourLabel.setImage(UIImage(named: "cross.png"), for: .normal)
            self.fiveLabel.setImage(UIImage(named: "cross.png"), for: .normal)
            self.sixLabel.setImage(UIImage(named: "cross.png"), for: .normal)
            self.sevenLabel.setImage(UIImage(named: "cross.png"), for: .normal)
            self.eightLabel.setImage(UIImage(named: "cross.png"), for: .normal)
            self.nineLabel.setImage(UIImage(named: "cross.png"), for: .normal)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先使用此代码(将Any更改为UIButton - 假设您在此处使用按钮,如果没有,请使用您的实际类型)

@IBAction func onePressed(_ sender: UIButton) {
    print("cell 1 was pressed while it showed \(shuffledArray[0])")
}

然后

if sender.tag == shuffledArray[0] {
  // the button's tag is the first one in the array
}

您可能希望保留索引(而不是使用上面的0)

添加属性

var buttonPressedIndex = 0

然后

if sender.tag == shuffledArray[buttonPressedIndex] {
  // the button's tag is the next one in the array
  buttonPressedIndex += 1 // go to next button in array
} else {
  // show an error
}