Search code examples
cocoaswift3nsbutton

NSButton state issues


I have two ViewControllers. On VC1 I have search criteria and on VC2 I have the search results. If you want to go from VC2 to VC1 the VC2 is dismissed.

On VC1 I have an NSButton(style Check, type Switch) which by default I want it to be in ON state. The purpose of the NSButton is to include photos in the results.

If the user unchecks the button and presses search, it will go on to VC2 showing the search results without photos.

BUT when the user goes back to VC1 for a new search that's where the unwanted behaviour occurs: The NSButton is unchecked(i want it to be checked by default, every time the user is at the VC1. Also, the button is nil.

Why is this happening, and how can i make it the button box to be ticked everytime the VC2 is dismissed?

I tried enabling it and setting it to ONState but as its nil it would crash.


Solution

  • I have managed to make it perform the way I want it by adding

    switch.state=1
    

    just before the segue from VC1 to VC2 is performed.

    However, I don't think this is the most elegant solution as the button is still nil.

    UPDATE: I have figured out that the issue occurs as when it goes from VC1 to VC2 the VC1 becomes nil, when the VC2 is dismissed it becomes nil as well. Hence the crash. One solution is to use delegates.

    VC1:

    class FirstViewController: UIViewController,SecondViewControllerProtocol {
    
    @IBOutlet var firstName: UITextField!
    @IBOutlet var lastName: UITextField!
    @IBOutlet var subscribeSwitch: UISwitch!
    
    @IBAction func goToSecondVC(_ sender: Any) {
        let viewController = self.storyboard?.instantiateViewController(withIdentifier: String(describing: SecondViewController.self)) as! SecondViewController
        viewController.delegate = self
        self.present(viewController, animated: true, completion: nil)
    
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
    
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func dismissViewController() {
        if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController"){
            subscribeSwitch.isOn=true
        }
    }
    
    }
    

    VC2:

    protocol SecondViewControllerProtocol {
    func dismissViewController()
    }
    
    
    
    class SecondViewController: UIViewController {
    
    var delegate:SecondViewControllerProtocol!
    
    
    @IBAction func goBackToFirstVC(_ sender: Any) {
        self.dismiss(animated: true) {
            self.delegate!.dismissViewController()
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    }