0

I am learning Swift by Apple's books. I am stuck at this problem. I'm trying to connect button programmatically, but I receive this problem.

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

Here is my code:

class ViewController: UIViewController {
    @IBOutlet var toggle: UISwitch!
    @IBOutlet var slider: UISlider!
    @IBOutlet var button: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        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)")
    }
/// not all ....

And additional photos: Task

Code

  • 5
    You haven't connected the button to the outlet; you can see the circle is empty next to the `@IBOutlet var button` You need to drag from the "new referencing outlet" to the button property as you have done for the other views. – Paulw11 Jun 21 '21 at 13:07

0 Answers0