2016-06-19 69 views
0

我正在進行測驗,其中存在文本字段並且用戶輸入答案。但是,我希望它能夠識別出正確答案後的空格仍然正確。例如,如果答案是紅色,並且用戶在紅色之後輸入一個空格,它仍然被認爲是正確的答案。以下是我的代碼。請讓我知道,並提前感謝您的所有幫助!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" 
    } 
} 
+0

@Alex這是如何重複我的其他問題?我的另一個問題是詢問如何在選擇正確答案後才顯示下一個問題按鈕。 這個問題是問我怎樣才能使測驗的文本框部分承認答案,即使在它的末尾添加空格 這些不是同一個問題 – Babz

+0

現在不記得鏈接背後的意圖是什麼這是錯誤的,因爲它指出了這個確切的問題。我已經刪除了我以前的評論。 – Alex

回答

1

你應該檢查面前人人平等修剪空白。您可以使用stringByTrimmingCharactersInSet:方法達到此目的。

func checkForCorrectAnswer() 
{ 
    let answer = enteredAnswer!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) 
    if answer.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 
} 
+0

謝謝!工作很好! – Babz

+0

@Babz:不客氣。快樂編碼! –

+0

@Babz如果這解決了你的問題,那麼你應該點擊複選標記來接受答案。 –

-1

你可以使用這樣的事情:

let str = "Hi How Are You" 
let newString = str.stringByReplacingOccurrencesOfString(" ", withString: "") 
print(newString) //This prints "HiHowAreYou" 

然後,只需比較一下兩者的用戶不帶空格回答正確的答案。在你的情況,你會使用這樣的:

if enteredAnswer.stringByReplacingOccurrencesOfString(" ", withString: "").lowercaseString == correctAnswer.stringByReplacingOccurrencesOfString(" ", withString: "").lowercaseString { 
//Answer Is Correct 

}

+0

我對編碼和swift有點新。我不是在抱歉。所以在我提供的代碼中,問題的正確答案是「斑塊」。我如何讓它認識到「斑塊」也是正確的(即斑塊後有空間)。如果你可以編輯我的代碼來告訴我這一點,將不勝感激 – Babz

+0

問:「什麼是正確的spellig:'文件名'或'文件名'?」怎麼能給出錯誤的答案? –