Swift UISwitch 第一次不工作

Swift UISwitch not working first time

我的视图控制器上有一个 UISwitch,我有它,所以当我切换它时,按钮的文本会发生变化。第一次我将它关闭再打开它不起作用,但如果你第二次尝试它,它就起作用了......我的代码中是否缺少某些东西?

        UISwitchOutlet.addTarget(self, action: #selector(MainPageViewController.switchChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)


func switchChanged(mySwitch: UISwitch) {
    let value = UISwitchOutlet.on
    if value {
        self.enterRoom.titleLabel?.text = "Enter Room"
    } else {
        self.enterRoom.titleLabel?.textAlignment = NSTextAlignment.Center
        self.enterRoom.titleLabel?.text = "Create"


    }
}

在下面试试。您的值将始终打开,因为您设置为打开状态。

func switchChanged(mySwitch: UISwitch) {
    if mySwitch.isOn {
        self.enterRoom.titleLabel?.text = "Enter Room"
    } else {
        self.enterRoom.titleLabel?.textAlignment = NSTextAlignment.Center
        self.enterRoom.titleLabel?.text = "Create"
    }
}