mardi 29 août 2017

Swift iOS - Relational Operator - UITextField .text

Developing a Single-View app for iOS using Swift 3 syntax to allow a UIButton to call on a function named CheckAnswer() that argues if the string value in a UITextField is equal to a specific value.

I keep receiving an error (unresolved identifier) when my CheckAnswer() function begins to execute a condition to argue the string value for my UITextField variable called Answer. I am not sure how to debug this error. :) Thanks for any suggestions!

   override func viewDidLoad() {
    super.viewDidLoad()

    //INPUT BOX - User Input!
    let Answer : UITextField = UITextField(frame: CGRect(x: 35, y: 150, width: 250, height:50))
    Answer.placeholder = "Enter Your Answer"
    Answer.font = UIFont.init(name:"times", size: 24)
    Answer.backgroundColor = UIColor(red:0xff ,green:0xff, blue:0x88 ,alpha:1)
    self.view.addSubview(Answer)

    //BUTTON - Check Answer!
    let Button : UIButton = UIButton(frame: CGRect(x: 35, y: 200, width: 250, height:50))
    Button.setTitle("Submit Answer", for: .normal)
    Button.titleLabel!.font = UIFont.init(name:"times", size: 24)
    Button.backgroundColor = UIColor(red:0x00 ,green:0x00, blue:0x00 ,alpha:1)
    Button.addTarget(self, action: #selector(CheckAnswer), for: .touchUpInside)
    self.view.addSubview(Button)
}

func CheckAnswer(_ sender: UIButton){
    if Answer.text == "answer" || Answer.text == "Answer"      
    { print("Correct ! Response") }
    else { print("Incorrect x Response") }
  }

Aucun commentaire:

Enregistrer un commentaire