Fabio Magarelli
Fabio Magarelli

Reputation: 1101

UISwitch crash on value change IOS Swift

I'm trying to set a simple UISwitch that simply copy is a boolean value (off or on) in a Bool Variable. The problem is that when I run my app, every time I change the switch value, it crashes with the following error (in the pic). I attach you the code I'm trying to use:

import UIKit

class TypeOfDiabetesVC: UIViewController {


    @IBOutlet weak var insulinDSwitch: UISwitch!

    var insulinsSwitchState : Bool = true

// viewDidLoad and Memory warning funcs etc...

@IBAction func insulinDSwitch(_ sender: UISwitch) {
        if (sender.isOn == true) {
            insulinDSwitch.setOn(true, animated: true)
        } else {
            insulinDSwitch.setOn(false, animated: true)
        }
        insulinsSwitchState = sender.isOn
    }

The app builds successfully but then when I tap on the switch to toggle it on or off, the app crash:

link to the image

Upvotes: 0

Views: 726

Answers (3)

Aju Antony
Aju Antony

Reputation: 639

Your method in TypeOfDiabetesVC View controller is named insulinDSwitch but the storyboard is trying to call InsulinDSwitch. The case difference is making the app crash.

Possible Solution : Remove the Action from storyboard and connect it again to the right method.

Upvotes: 1

sanjaykmwt
sanjaykmwt

Reputation: 605

You have problems with your outlets please check the outlet of the action you have attached with the UISwitch.

Upvotes: 0

Vishal Kumar
Vishal Kumar

Reputation: 16

This crash is because you haven't connected the IBAction with storyboard, Please do it again.

Upvotes: 0

Related Questions