Post

Replies

Boosts

Views

Activity

Reply to How do I connect a control to a method programmatically? - Develop In Swift Fundamentals - Programmatic Actions
Thank you @Claude31 for your continued help. To answer your questions. "That means you have a button in the view in Interface Builder. Right ?", "Did you do this?" Yes, I do have a button in the view in interface builder. No, I did not drag to connect because the exercise is teaching me how to connect programmatically, not by ⌃ + click + drag. However, your solution does work. If I ⌃ + click + drag from the empty circle to the button, it does connect. But repeating once again, I'm not trying to use the ⌃ + click + drag feature. I want to connect just by typing code (programmatically, I think it's called). This is my code. import UIKit class ViewController: UIViewController {     @IBOutlet var toggle: UISwitch!     @IBOutlet var slider: UISlider!     @IBOutlet var button: UIButton!     override func viewDidLoad() {         super.viewDidLoad()         /*Do any additional setup after loading the view.*/         button.addTarget(self, action: #selector(buttonTapped(_:)),            for: .touchUpInside)     }     @IBAction func buttonTapped(_ sender: Any) {         print("Button was tapped!")         if toggle.isOn {             print("The switch is on!")         } else {             print("The switch is off.")         }         print("The slider is set to \(slider.value).")     }     @IBAction func switchToggled(_ sender: UISwitch) {         if sender.isOn {             print("Switch is on!")         } else {             print("Switch is off!")         }     }     @IBAction func sliderValueChanged(_ sender: UISlider) {         print(sender.value)     }          @IBAction func keyboardReturnKeyTapped(_ sender: UITextField) {         if let text = sender.text {             print(text)         }     }     @IBAction func textChanged(_ sender: UITextField) {         if let text = sender.text {             print(text)         }     }     @IBAction func respondToTapGesture(_ sender: UITapGestureRecognizer) {         let location = sender.location(in: view)         print(location)     } }
Mar ’21