禁用和启用 UIButton - XCode Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26985027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable and Enable UIButton - XCode Swift
提问by MitchKrendell
I have made an app that generates a random number and the user must enter a guess and click a button, GUESS to submit. All is working fine. However, I am simply trying to start the app with btnGuess disabled and enabled when a new game is started. The button will also be disabled when the user wins the game. So far, I have searched online and on here as to how you disabled/enable a UIButton and every answer I have found (most notably: button.enabled = false) does not seem to work. Please let me know what I am missing here. I have also tried button.enabled = NO; (which was another offered solution that does not work for me). Thanks.
我制作了一个生成随机数的应用程序,用户必须输入猜测并单击按钮 GUESS 才能提交。一切正常。但是,我只是尝试在启动新游戏时禁用并启用 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
}
}
}
回答by A. R. Younce
The variable you're referencing (btnGuess
) is declared as a function in your code here:
您引用的变量 ( btnGuess
) 在您的代码中被声明为一个函数:
@IBAction func btnGuess(sender: UIButton) {
You may want to change the action function's name to @IBAction func guess(sender: UIButton)
and then declare a @IBOutlet weak var btnGuess: UIButton!
property.
您可能希望将操作函数的名称更改为@IBAction func guess(sender: UIButton)
,然后声明一个@IBOutlet weak var btnGuess: UIButton!
属性。
回答by Seyyed Parsa Neshaei
It is happening because btnGuess is a function, not a variable.
这是因为 btnGuess 是一个函数,而不是一个变量。
Change this code:
更改此代码:
@IBAction func btnGuess(sender:UIButton)
To:
到:
@IBAction func btnGuessClicked(Sender:UIButton)
And also make an @IBOutlet for btnGuess.
并为 btnGuess 制作一个@IBOutlet。
回答by Gutty1
my suggestion will be to create also a:
我的建议是创建一个:
@IBOutlet weak var btnGuess: UIButton!
in your view controller also attached to the same button and in the code add whenever relevant the line:
在您的视图控制器中也附加到同一个按钮,并在代码中添加相关行:
btnGuess.enabled = false
and that should do the trick for you
这应该对你有用
回答by Krivvenz
You have the function of the button setup (the Action) but no way to reference the actual appearance of the button.
您拥有按钮设置(Action)的功能,但无法参考按钮的实际外观。
You need to add more IBOutlets at the top of your class for your buttons:
您需要在类的顶部为按钮添加更多 IBOutlets:
import UIKit
class myViewController: UIViewController {
@IBOutlet weak var btnStart: UIButton!
@IBOutlet weak var btnGuess: UIButton!
@IBAction func btnStart(sender: UIButton) {
btnGuess.enabled = true
}
}