如何使用SWITCH和uiTextfield变量更新swift中的标签

时间:2015-09-18 16:46:44

标签: ios swift switch-statement case

我想制作一个小程序,当一个人在文本字段中键入“happy”或“sad”时,标签会根据案例更新并说出一些内容......这是我的代码:

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";




        }


    }

1 个答案:

答案 0 :(得分:0)

我认为您的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";

    }