gpt4 book ai didi

swift - UILabel 返回 "nil"?

转载 作者:行者123 更新时间:2023-11-30 10:18:15 25 4
gpt4 key购买 nike

我的 UIButton 遇到问题。例如,当我单击 UIButton Number3 并点击 ViewController 上的 checkButton 时,currentCountLabel 中的结果是可选的(3)。在我单击 checkButton 后,它会对每个数字执行此操作。如果我再次单击该按钮,currentCountLabel 会显示“nil”。这是我的全部代码:

ViewController.swift:

class ViewController: UIViewController {

var currentCountLabel: UILabel = UILabel()

var currentCount: Int = 0

var IncorrectLabel: UILabel = UILabel()

var CorrectLabel: UILabel = UILabel()

var currectAmountCorrect : Int = 0

var currentAmountIncorrect : Int = 0

var flashTimer : NSTimer = NSTimer()

var timerCounter : Int = 0

var randomImageGeneratorNumber : Int = 0

var flashingImageView: UIImageView = UIImageView()

var flashButton: UIButton = UIButton()

var currentTime = NSTimer()

func timerFunc() {
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
}

@IBAction func flashButton(sender: UIButton) {

flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
flashingImageView.image = UIImage(named: "Image\(arc4random_uniform(6) + 1).png")

if (flashingImageView.image == UIImage(named: "Image1")){

randomImageGeneratorNumber == 1

}

if (flashingImageView.image == UIImage(named: "Image2")){

randomImageGeneratorNumber == 2

}

if (flashingImageView.image == UIImage(named: "Image3")){

randomImageGeneratorNumber == 3

}

if (flashingImageView.image == UIImage(named: "Image4")){

randomImageGeneratorNumber == 4

}

if (flashingImageView.image == UIImage(named: "Image5")){

randomImageGeneratorNumber == 5

}

if (flashingImageView.image == UIImage(named: "Image6")){

randomImageGeneratorNumber == 6

}

currentCount = 0
currentCountLabel.text = "\(currentCount)"

if (flashButton.hidden == true){
flashButton.hidden = true;
flashingImageView.hidden = false
}

var currentTime = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "timerFunc", userInfo: nil, repeats: false)

func timerFunc(){
self.flashingImageView.hidden = true;
flashButton.hidden = !flashButton.hidden
flashingImageView.hidden = !flashingImageView.hidden
}

}

@IBAction func checkButton(sender: UIButton) {


if (flashButton.hidden == true){
flashButton.hidden == false
flashingImageView.hidden == true
}

var currentCount = currentCountLabel.text?.toInt()
if randomImageGeneratorNumber == currentCount {
currectAmountCorrect += 1
CorrectLabel.text = "\(currectAmountCorrect)"

} else {
currentAmountIncorrect += 1
IncorrectLabel.text = "\(currentAmountIncorrect)"
}

if (currentCountLabel.text == "0"){

let alert = UIAlertView()
alert.title = "Alert"
alert.message = "You must type in an answer in order to check it"
alert.addButtonWithTitle("Understood")
alert.show()

}
currentCount == 0
currentCountLabel.text = "\(currentCount)"
}


@IBAction func clearButton(sender: UIButton) {
currentCount = 0
currentCountLabel.text = "\(currentCount)"
}


@IBAction func Number1Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 1
currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number2Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 2
currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number3Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 3
currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number4Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 4
currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number5Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 5
currentCountLabel.text = "\(currentCount)"
}

@IBAction func Number6Button(sender: UIButton) {
currentCount = currentCount * 10
currentCount = currentCount + 6
currentCountLabel.text = "\(currentCount)"
}


override func viewDidLoad() {
super.viewDidLoad()
var currentCountLabel = "\(currentCount)"
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

我不知道发生了什么或如何解决它。

如有任何意见和建议,我们将不胜感激。

提前致谢。

最佳答案

如果您阐明您希望它如何表现,将会有所帮助。如果您想知道为什么它是可选的以及为什么它返回 nil 那么您可以按如下方式修复此问题。

在您的 checkButton() 函数中,末尾有这一行:

currentCountLabel.text = "\(currentCount)"

currentCount 变量解包为:

currentCountLabel.text = "\(currentCount!)"

在同一个 checkButton() 函数中,您有以下行:

currentCount == 0

因此,如果您想知道为什么它不返回 0,那是因为您使用的是比较运算符而不是赋值运算符。

关于swift - UILabel 返回 "nil"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28952609/

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