禁用和启用 UIButton - XCode Swift [英] Disable and Enable UIButton - XCode Swift

查看:26
本文介绍了禁用和启用 UIButton - XCode Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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) 在此处的代码中声明为函数:

The variable you're referencing (btnGuess) is declared as a function in your code here:

@IBAction func btnGuess(sender: UIButton) {

您可能希望将操作函数的名称更改为 @IBAction func guess(sender: UIButton),然后声明一个 @IBOutlet weak var btnGuess: 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.

这篇关于禁用和启用 UIButton - XCode Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆