【问题标题】:how to update label in swift using SWITCH and a uiTextfield variable如何使用 SWITCH 和 uiTextfield 变量快速更新标签
【发布时间】:2015-09-18 16:46:44
【问题描述】:

我想制作一个小程序,当一个人在文本字段中输入“快乐”或“悲伤”时,标签会根据情况更新并说些什么……这是我的代码:

class ViewController: UIViewController {

    @IBOutlet weak var aiAnswer: UILabel!

    @IBOutlet weak var humanResponse: UITextField!

    // when user taps submit run this

    @IBAction func responseRequest(sender: AnyObject) {

    // check the variable humanResponse to see if they typed happy or sad and update the UI label based on that

        switch (humanResponse) {

        case "happy":

                aiAnswer.text = "thats great your happy!";

        case "sad":

                aiAnswer.text = "thats no fun!";

        default:

                aiAnswer.text = "please input a valid emotions";




        }


    }

【问题讨论】:

    标签: ios swift switch-statement case


    【解决方案1】:

    我认为您的switch 案例有问题。像这样更新它:

    switch humanResponse.text! {   //text entered by user
    
        case "happy":
    
            aiAnswer.text = "thats great your happy!";
    
        case "sad":
    
            aiAnswer.text = "thats no fun!";
    
        default:
    
            aiAnswer.text = "please input a valid emotions";
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多