Ios 如何使用SWITCH和uiTextfield变量更新swift中的标签
我想制作一个小程序,当一个人在文本字段中键入“happy”或“sad”时,标签会根据情况进行更新,并显示一些内容。。。这是我的密码:Ios 如何使用SWITCH和uiTextfield变量更新swift中的标签,ios,swift,switch-statement,case,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 @IB
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";
}
}
我认为您的
开关有问题。更新如下:
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";
}