0

セグエ経由で2つViewControllers接続しています。の値を からにShow渡す必要があります。NSSliderViewControllerSecondViewCotroller

ViewControllerそのため、変数内のスライダーを動かすと で更新されSecondViewControllerます。

変数の値を更新するにはimagesQty?

// FIRST VIEW CONTROLLER

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var slider: NSSlider!
    @IBOutlet weak var photosLabel: NSTextField!

    @IBAction func segueData(_ sender: NSSlider) {
        photosLabel.stringValue = String(slider.intValue) + " photos"
        self.performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "SegueIdentifierForSecondVC"), sender: slider)
    }

    func prepare(for segue: NSStoryboardSegue, sender: NSSlider?) {
        if segue.identifier!.rawValue == "SegueIdentifierForSecondVC" {
            if let secondViewController =
            segue.destinationController as? SecondViewController {
                secondViewController.imagesQty = slider.integerValue
            }
        }
    }
}

// SECOND VIEW CONTROLLER

import Cocoa

class SecondViewController: NSViewController {

    var imagesQty = 30  

    override func viewWillAppear() {

    super.viewWillAppear()
    self.view.wantsLayer = true

    print("viewWillAppear – Qty:\(imagesQty)")

    //let arrayOfViews: [NSImageView] = [view01...view12]

    let url = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Desktop/ArrayOfElements")
    do {
        let fileURLs = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]).reversed()
        let photos = fileURLs.filter { $0.pathExtension == "jpg" }

        for view in arrayOfViews {
            //"imagesQty" is here
            let i = Int(arc4random_uniform(UInt32(imagesQty-1)))
            let image = NSImage(data: try Data(contentsOf: photos[i]))
            view.image = image
            view.imageScaling = .scaleNone
        }
    } catch {
        print(error)
    }
}

ここに画像の説明を入力 ここに画像の説明を入力

4

2 に答える 2