我正在做一個測驗,對這個問題有多個正確的答案。例如,在下面的代碼中,對於「病變的顏色是什麼」的問題,正確答案是「粉紅色」。但我也希望它能夠認識到紅色也將被視爲正確的答案。有沒有一種方法可以爲一個問題編碼多個正確答案?任何幫助將不勝感激。謝謝!Swift - 如何讓文本字段在測驗中識別多個正確答案?
import Foundation
import UIKit
class Case1Q2ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var imageButton: UIButton!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet var questionLabel: UILabel!
@IBOutlet var correctAnswerLabel: UILabel!
@IBOutlet var inputTextField: UITextField!
var enteredAnswer: String?
var correctAnswers = ["Pink", "Red"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Case1Q2ViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Case1Q2ViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
inputTextField.delegate = self
titlesForLabels()
nextButton.enabled = false
imageButton.layer.cornerRadius = 2;
imageButton.layer.borderWidth = 2;
imageButton.layer.borderColor = UIColor.blackColor().CGColor
questionLabel.layer.cornerRadius = 2;
questionLabel.layer.borderWidth = 2;
questionLabel.layer.borderColor = UIColor.blackColor().CGColor
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func titlesForLabels() {
questionLabel.text = "What is the colour of the lesion(s)?"
correctAnswerLabel.text = correctAnswers.joinWithSeparator(", ")
correctAnswerLabel.hidden = true
inputTextField.text = nil
inputTextField.enabled = true
}
func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: {() -> Void in
self.view.frame.origin.y = -keyboardFrame.size.height
})
}
func keyboardWillHide() {
UIView.animateWithDuration(0.1, animations: {() -> Void in
self.view.frame.origin.y = 0
})
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
enteredAnswer = textField.text
checkForCorrectAnswer()
return true
}
func checkForCorrectAnswer() {
let answer = enteredAnswer!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
if correctAnswers.contains(answer.lowercaseString) {
print("Correct")
correctAnswerLabel.textColor = UIColor.greenColor()
correctAnswerLabel.text = "Correct!"
nextButton.enabled = true
} else {
print("Wrong Answer")
correctAnswerLabel.textColor = UIColor.redColor()
correctAnswerLabel.text = "Incorrect! Please try again"
nextButton.enabled = false
}
correctAnswerLabel.hidden = false
}
override func viewWillAppear(animated: Bool) {
navigationItem.title = "Case 1: Q2"
}
}
使'correctAnswer'爲所有可接受答案的數組並檢查是否correctAnswer .contains(enteredAnswer)' – dan
@dan,我對編碼和swift很陌生。有什麼方法可以讓我看看我的代碼? – Babz
@Babz:歡迎來到StackOverflow,Programming和Swift!如果我可以提供一個建議,那麼在請求某人爲您寫(尤其是簡單的)之前,您可以親自實施一個解決方案。 Apple的遊樂場使這一點變得特別簡單,因爲您可以實時看到代碼的輸出。祝你好運,希望你做一個殺手級應用程序:) –