我有一套步进器,而不是加起来他们应该给出一个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分)
答案 0 :(得分:0)
您可能无法"禁用+侧" ...
选项:
使用两个按钮,而不是步进器。禁用" +"根据需要按钮,或
设置增量图像以指示在您要禁用它时禁用它,并且不要处理" +"抽头。
UIStepper
的每一边都会在达到最小值或最大值时自动禁用。请参阅@ the4kman的答案
答案 1 :(得分:0)