disable button until user click switch On

i can't simple do


@IBAction func MySwitch(_ sender: UISwitch) {
        if sender.isOn {
            MyButton(sender: UIBarButtonItem) = true /*Cannot assign to value: function call returns immutable value*/
        }
        else{
            MyButton(sender: UIBarButtonItem) = false /* Cannot assign to value: function call returns immutable value*/
        }

    }


i ketp gettin this and i don't know what to put there

(sender: UIBarButtonItem). <---- MyButton


and this error

Cannot assign to value: function call returns immutable value


all the code

import UIKit
class ViewController: UIViewController {

    @IBAction func MyButton(_ sender: UIBarButtonItem) {

    }

    @IBAction func MySwitch(_ sender: UISwitch) {
        if sender.isOn {
            MyButton(sender: UIBarButtonItem) = true
        }
        else{
           MyButton(sender: UIBarButtonItem) = false
        }
   
    }

    override func viewDidLoad() {
        super.viewDidLoad()
   
        /
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
     @IBAction func unwindToVC1(segue:UIStoryboardSegue) {}
    }
Accepted Answer

So, you want to do something with the button when you click the switch ?

What would you do in MyButton ?

As you try to set to true or false, I understand you want to change some button property as its state ?


Anyway, you cannot do it like this.

Note: func names should start with lowercase, so I changed it.


You have to declare IBOutlets and connect it to the button in IB

@IBOutlet weak var myButton : UIButton!


Then use it in MySwitch, for whatever purpose


    @IBAction func mySwitch(_ sender: UISwitch) {
        if sender.isOn {
            myButton.isOn = true  //   MyButton(sender: UIBarButtonItem) = true
        }
        else {
           myButton.isOn = false  //  MyButton(sender: UIBarButtonItem) = false
        }
    }

thanks🙂

disable button until user click switch On
 
 
Q