Good day! Tell me how to change the image in this outlet by condition (or, as I understand it, hide one, show the other). Outlets called image1 and image2. They must change according to the condition for entering the number of characters in the textField!
2 answers
As I understand you created 2 pictures and want to show only one of them:
if (textField!.text.characters.count > 5) { image1.hidden = true image2.hidden = false } else { image1.hidden = false image2.hidden = true } You can do with one image view:
if (textField!.text.characters.count > 5) { // эта строчка разная в зависимости от версии swift image1.image = UIImage(named:"imageX") } else { image1.image = UIImage(named:"imageY") } - Thanks about the competent programmer! Works! - Igor Ivanov
- @IgorIvanov if works accept the answer - Max Mikheyenko
|
Hello! I write the answer for Swift 3. You create 3 outlets 2 for photos, 1 for textField. Then you create - IBAction - Send Events - Editing Changed. How to do it? - You press the right button on the text field, the options appear - there you find editing Changed and underneath the Sent Events you draw on the UIViewController. Now, at any change of a field there will be an event.
I enclose all the code.
import UIKit class ViewController: UIViewController { @IBOutlet var image1: UIImageView! @IBOutlet var image2: UIImageView! @IBOutlet var textField: UITextField! @IBAction func textFieldEditingChanged(_ sender: Any) { let length = textField.text?.characters.count if length! > 5 { image1.isHidden = true image2.isHidden = false } else { image1.isHidden = false image2.isHidden = true } } } |