Reputation: 55
I am currently using Swift 4 and struggling with a problem which probably is quite simple but I can't find a simple solution to it. I'm gonna try to explain it as thorough as I can.
By using a text field I want to assign an action to the Send button on the virtual keyboard to submit the input text and then I want to clear the text field. After the text have been submitted I want to check if the submitted text is equal to a specified String (for example "hello, playground"). If the submitted text is equal to "hello, playground" and thereby correct, I want to update a label to tell the user "Correct"
So the process looks like this:
Text field -> Input text -> Submit text using virtual Send button -> Clear text field -> Check if submitted text = specified String -> If yes, update label to "Correct" -> If no, do nothing -> repeat until correct.
I hope it is understandable and thanks in advance.
Upvotes: 1
Views: 453
Reputation: 2666
Assumptions:
textField
label
You can check it in that button action function like following:
if let text = textField.text {
if text == "Specified Text" {
label.text = "Correct"
}
}
So if textField's text is equal to your specified text label's text will be "Correct", otherwise nothing will happen.
If by virtual send button you mean the return button of keyboard, you need to do the following in textFieldShouldReturn
delegate method:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let text = textField.text {
if text == "Specified text" {
label.text = "Correct"
}
textField.text = ""
}
textField.resignFirstResponder() // this is optional, you might wanna hide keyboard or not
return true
}
Upvotes: 1