[Solved] How to update NSImageView using NSSlider?


First of all be aware that any move of the slider performs a new segue. To avoid that declare a boolean property which is set when the segue is performed the first time and could be reset after the second view controller has been dismissed.

To update the value in the second view controller keep the reference and call a method

Actually with this code you don’t need the slider IBOutlet

class ViewController: NSViewController {


    var secondControllerIsPresented = false
    var secondController : SecondViewController?

...


    @IBAction func segueData(_ sender: NSSlider) {

        if !secondControllerIsPresented {
            self.performSegue(withIdentifier: .secondVC, sender: sender)
            secondControllerIsPresented = true
        } else {
            secondController?.updateArray(value: sender.integerValue)
        }
    }

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
        if let identifier = segue.identifier, identifier == .secondVC {
            let secondViewController = segue.destinationController as! SecondViewController
            secondController = secondViewController
            let slider = sender as! NSSlider
            secondViewController.imagesQty = slider.integerValue
        }
    }

...

class SecondViewController: NSViewController {

...


   func updateArray(value : Int) {
       print(value)
   }

Honestly I would use a button to perform the segue and move the slider into the second view controller. To shuffle the array use an Array extension and as view an NSCollectionView rather than a bunch of image views

2

solved How to update NSImageView using NSSlider?