当写出正确的答案时,如何使测验的答案标记显示正确,如果写错了答案,怎么办?

时间:2016-06-19 02:52:09

标签: ios iphone swift

那么当只写出正确的答案时,如何只允许下一个问题按钮出现?我正在使用Swift!以下是我的代码。任何帮助将不胜感激!谢谢!

这是我的代码和     导入UIKit

class InputViewController1: UIViewController, UITextFieldDelegate {

@IBOutlet var questionLabel: UILabel!

@IBOutlet var correctAnswerLabel: UILabel!

@IBOutlet var inputTextField: UITextField!

var enteredAnswer: String?

var correctAnswer = "Correct Answer = Small"

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()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func titlesForLabels() {
    questionLabel.text = "What is the size of the lesion(s)?"
    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()
    } else {
        print("Wrong Answer")
        correctAnswerLabel.textColor = UIColor.redColor()

    }
    correctAnswerLabel.hidden = false
}

}

1 个答案:

答案 0 :(得分:0)

在提问之前,请仔细研究此代码。使用addTarget UIControlEvents.EditingChanged的{​​{1}}方法,您可以使用.EditingChanged的实时当前值更改InputViewController的属性。这是在inputTextField函数中完成的。另请注意,textDidChangenexButton.alpha中设置为0.0,并在viewDidLoadtextDidChange中相应更改。

checkForCorrectAnswer