2015-09-18 77 views
-1

我想製作一個小程序,當人們在文本字段中鍵入「開心」或「難過」時,標籤將根據案例進行更新並說出一些內容。 ..這裏是我的代碼:如何使用SWITCH和uiTextfield變量快速更新標籤

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




     } 


    } 

回答

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

    }