gpt4 book ai didi

xcode - 禁用和启用 UIButton - XCode Swift

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

我制作了一个生成随机数的应用程序,用户必须输入猜测并单击“GUESS”按钮才能提交。一切正常。但是,我只是尝试在启动新游戏时禁用 btnGuess 并启用 btnGuess 来启动应用程序。当用户赢得游戏时,该按钮也将被禁用。到目前为止,我已经在网上和这里搜索了如何禁用/启用 UIButton ,我找到的每个答案(最值得注意的是:button.enabled = false)似乎不起作用。请让我知道我在这里缺少什么。我也尝试过button.enabled = NO; (这是另一个提供的解决方案,但对我不起作用)。谢谢。

import UIKit

class myViewController: UIViewController {

var guesses : UInt = 0
var number : UInt32 = 0
var inputGuess = 0


@IBOutlet weak var lblGuesses: UILabel!

@IBOutlet weak var lblGuessTitle: UILabel!

@IBOutlet weak var lblOutput: UILabel!

@IBOutlet weak var txtInput: UITextField!

@IBAction func txtInput(sender: UITextField){

}

override func viewDidLoad() {
super.viewDidLoad()

txtInput.enabled = false
// btnGuess.enabled = false //doesnt work



// Do any additional setup after loading the view.
}

func generateRandom() -> UInt32{
return arc4random_uniform(100) + 1
}

func incrementGuesses(){
++guesses
lblGuesses.text = String(guesses)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func btnGuess(sender: UIButton) {

let inputGuess = txtInput.text.toInt()

if UInt32(inputGuess!) > number{
lblOutput.text = "Too High!"
incrementGuesses()
}
else if UInt32(inputGuess!) < number{
lblOutput.text = "Too Low!"
incrementGuesses()
}else{
lblOutput.text = "Correct Guess! You Win!"
guesses = 0
txtInput.text = ""
txtInput.enabled = false
//btnGuess.enabled = false //doesnt work

}
}


@IBAction func btnStart(sender: UIButton) {

number = generateRandom()
lblOutput.text = "I'm thinking of a number between 0 - 100"
lblGuesses.text = "0"
txtInput.enabled = true
//btnGuess.enabled = true //doesnt work
}

}

最佳答案

您引用的变量 (btnGuess) 在您的代码中声明为函数:

@IBAction func btnGuess(sender: UIButton) {

您可能需要将操作函数的名称更改为@IBAction func Guess(sender: UIButton),然后声明一个@IBOutlet weak var btnGuess: UIButton! 属性。

关于xcode - 禁用和启用 UIButton - XCode Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26985027/

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