我正在進行測驗,其中存在文本字段並且用戶輸入答案。但是,我希望它能夠識別出正確答案後的空格仍然正確。例如,如果答案是紅色,並且用戶在紅色之後輸入一個空格,它仍然被認爲是正確的答案。以下是我的代碼。請讓我知道,並提前感謝您的所有幫助!Swift - 如何讓textfield在仍然正確的答案輸入正確的答案後仍識別空格
import Foundation
import UIKit
class Case1Q3ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var nextButton: UIButton!
@IBOutlet var questionLabel: UILabel!
@IBOutlet var correctAnswerLabel: UILabel!
@IBOutlet var inputTextField: UITextField!
var enteredAnswer: String?
var correctAnswer = "Well demarcated"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(InputViewController1.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(InputViewController1.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
inputTextField.delegate = self
titlesForLabels()
nextButton.enabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func titlesForLabels() {
questionLabel.text = "Describe the borders"
correctAnswerLabel.text = correctAnswer
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() {
if enteredAnswer!.lowercaseString == correctAnswer.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 - Q3"
}
}
@Alex這是如何重複我的其他問題?我的另一個問題是詢問如何在選擇正確答案後才顯示下一個問題按鈕。 這個問題是問我怎樣才能使測驗的文本框部分承認答案,即使在它的末尾添加空格 這些不是同一個問題 – Babz
現在不記得鏈接背後的意圖是什麼這是錯誤的,因爲它指出了這個確切的問題。我已經刪除了我以前的評論。 – Alex