那么当只写出正确的答案时,如何只允许下一个问题按钮出现?我正在使用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
}
}
答案 0 :(得分:0)
在提问之前,请仔细研究此代码。使用addTarget
UIControlEvents.EditingChanged
的{{1}}方法,您可以使用.EditingChanged
的实时当前值更改InputViewController
的属性。这是在inputTextField
函数中完成的。另请注意,textDidChange
在nexButton.alpha
中设置为0.0
,并在viewDidLoad
和textDidChange
中相应更改。
checkForCorrectAnswer