禁用UIStepper的一半

时间:2017-12-19 13:57:52

标签: ios swift xcode delegates uistepper

我有一套步进器,而不是加起来他们应该给出一个X值。它们都在UICollectionview中,我使用委托在它们和ViewController之间传递值。
你应该在不同的玩家之间分配积分,一旦你达到分配给你的积分总数,你就不能再添加"添加"点。我想禁用" half"一旦达到总点数,就进入步进器。我怎样才能做到这一点? (不禁用整个步进器,因为用户可能想要重新分配点并返回一些点)。

到目前为止,这是我的代码:

    protocol CollectionVCDelegate: class {
        func usedPoints() -> Int
        func returnPoints() -> Int
    }
    class PointAllocationVC: UIViewController, CollectionVCDelegate {

        func usedPoints() -> Int {
            pointsToAllocate -= 1
            totalPointsLabel.text = String(pointsToAllocate)
            return pointsToAllocate
        }

        func returnPoints() -> Int
        {
            pointsToAllocate += 1
            totalPointsLabel.text = String(pointsToAllocate)
            return pointsToAllocate
        }
    var pointsToAllocate: Int = 5 //may change, 5 for example
    @IBOutlet weak var ptsAllocView: UIView!
    @IBOutlet weak var totalPointsLabel: UILabel!
    @IBOutlet weak var addAllButton: UIButton!
    @IBOutlet weak var ptAllocCollectionView: UICollectionView!
}

extension PointAllocationVC: UICollectionViewDelegate, UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return currentPlayers.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let myptCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ptsCell", for: indexPath) as! PtsAllocationViewCell
        myptCell.playerNameLabel.text = currentPlayers[indexPath.row]
        myptCell.playerScoreLabel.text = "Score: \(currentScore[indexPath.row])"
        myptCell.delegate = self

        if indexPath.row == 0
        {
            myptCell.ptsAllocStepper.minimumValue = 0
        }
        else
        {
            myptCell.ptsAllocStepper.maximumValue = 0
            myptCell.isSelf = false
        }

        return myptCell
    }
}

现在,这里是ViewCells的代码:

import UIKit

class PtsAllocationViewCell: UICollectionViewCell {
    var delegate: CollectionVCDelegate? = nil
    var isSelf = true
    var ptsRemaining: Int = 0
    @IBOutlet weak var ptsLabel: UILabel!
    @IBOutlet weak var playerNameLabel: UILabel!
    @IBOutlet weak var playerScoreLabel: UILabel!
    @IBOutlet weak var ptsAllocStepper: UIStepper!
    @IBAction func stepperTapped(_ sender: UIStepper) {
        let myInt: Int = Int(ptsLabel.text!)!
        if delegate != nil
        {
            if isSelf
            {
                if myInt > Int(sender.value)
                {
                    ptsRemaining = (delegate?.returnPoints())!
                }
                else
                {
                    ptsRemaining = (delegate?.usedPoints())!
                }

            }
            else
            {
                if myInt > Int(sender.value)
                {
                    ptsRemaining = (delegate?.usedPoints())!
                }
                else
                {
                    ptsRemaining = (delegate?.returnPoints())!
                }
            }
        }
        ptsLabel.text = String(Int(sender.value))
    }

}

注意:此代码的工作范围与我想要的一样多(就像从ViewController中添加/减去pointsToAllocate并更新标签和所有。但是,截至目前,没有锁定阻止用户表单过度使用积分(比如说他每人可以加5分,最后总共得到-15分,你不应该低于0分)

只是为了增加清晰度,这里有一张图片,请注意它将有大约4-6个不同的"玩家": storyboard of ViewController

2 个答案:

答案 0 :(得分:0)

您可能无法"禁用+侧" ...

选项:

  1. 使用两个按钮,而不是步进器。禁用" +"根据需要按钮,或

  2. 设置增量图像以指示在您要禁用它时禁用它,并且不要处理" +"抽头。

  3. UIStepper的每一边都会在达到最小值或最大值时自动禁用。请参阅@ the4kman的答案

答案 1 :(得分:0)

设置所有步进器' maximumValue到总点数,减去每次步进器值更改时已分配的点数。你应该注意UIControlEvents.valueChanged

这样,如果达到最大点数,步进器的 + 一半将被禁用。

UIStepper with + half disabled