gpt4 book ai didi

ios - 禁用一半的 UIStepper

转载 作者:行者123 更新时间:2023-11-30 11:55:13 24 4
gpt4 key购买 nike

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

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

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

现在,这是 ViewCell 的代码:

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 中添加/减去 PointToAllocate 以及更新标签等而言)。但是,到目前为止,还没有任何锁可以防止用户表单过度使用分(假设他可以在每项上打 5 分,最后总分是 -15 分,你不应该低于 0 分)

为了更加清晰起见,这里有一张图片,请注意它将有大约 4 - 6 个不同的“玩家”: storyboard of ViewController

最佳答案

将所有步进器的 maximumValue 设置为总点数,减去每次步进器值发生变化时已分配的点数。您应该为此观察 UIControlEvents.valueChanged

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

UIStepper with + half disabled

关于ios - 禁用一半的 UIStepper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47888592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com