gpt4 book ai didi

ios - UISegmentedControl 和 .selectedSegmentIndex 值错误?

转载 作者:行者123 更新时间:2023-11-28 11:13:41 25 4
gpt4 key购买 nike

我的代码中有一个包含 3 个元素的简单段。出于测试目的,我还有一个变量,它会根据我按下的段数 (3) 而递增。该变量的值打印在 UITextView 中。这是代码:

import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var segment: UISegmentedControl!
@IBOutlet weak var prwtoView: UIView!
@IBOutlet weak var prwtoText: UITextField!

var i : Int = 0

override func viewWillAppear(animated: Bool)
{
prwtoText.backgroundColor = UIColor.purpleColor()
prwtoText.textColor = UIColor.whiteColor()
segment.setTitle("Zero", forSegmentAtIndex: 0)
segment.addTarget(self, action: "action", forControlEvents: .ValueChanged)
segment.insertSegmentWithTitle("random", atIndex: 2, animated: false)
}
func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
i = 0
}
if argumentForSegment == 1
{
i += 2
}
else
{
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}

}

虽然我知道它以值 -1 开头,但我不希望我的应用程序在未按下时执行任何操作。问题是,即使我按下第一段 (0) 并且它应该使 i = 0 它也不会那样做,尽管如果我打印 argumentForSegment我的终端确实显示 0 作为值。最后,每次我按下零段 (0) 时,我的 i 值都不会变为 0。也许我使用了错误的 UISegmentedControl 方法?

编辑:通过更改以下代码修复它:

func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
i = 0
}
if argumentForSegment == 1
{
i += 2
}
else
{
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}

到:

func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
i = 0
}
if argumentForSegment == 1
{
i += 2
}
else if argumentForSegment == 2 // <==== here
{
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}

有人可以解释为什么它使用 else 的优先级,尽管在打印 argumentForSegment 时值为零吗?换句话说,为什么当我有一个单独的 else 作为 argumentForSegment == 0 的值时,它选择了 else 而不是第一条语句?

最佳答案

Could someone explain why it used the priority of else although the value was zero when printing argumentForSegment? In other words why when I had an else alone for the value of argumentForSegment == 0 it chose the else instead of the first statement?

当您遇到代码未按预期运行的情况时,在调试器中单步执行它或添加一些诊断性 print 语句会很有帮助。

例如:

func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
print("In first block")
i = 0
}
if argumentForSegment == 1
{
print("In second block")
i += 2
}
else
{
print("In third block")
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}

如果这样做,您会注意到当 argumentForSegment0 时,输出将是:

In first blockIn third block

So, the problem is not that it is choosing the third block over the first. The problem is that it is doing both. You want it to stop after it has detected that argumentForSegment is 0, so add an else to the second conditional statement so that it only does that when the first conditional statement failed:

func action()
{
let argumentForSegment = segment.selectedSegmentIndex
if argumentForSegment == 0
{
i = 0
}
else if argumentForSegment == 1 // added "else" here
{
i += 2
}
else
{
i += Int(arc4random_uniform(100))
}
print(argumentForSegment)
prwtoText.text = "\(i)"
}

关于ios - UISegmentedControl 和 .selectedSegmentIndex 值错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33302875/

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