2

I have an NSViewController defined in my storyboard that contains a view that holds various labels and a button. They are aligned using auto layout. enter image description here

I want to use this as a UI prototype so that I can generate lots of these and add them to a vertical NSStackView much like table rows.

I have a subclass of my NSViewController defined like this:

class ApproachRowVC: NSViewController {

  @IBOutlet var quantityLabel: NSTextField!
  @IBOutlet var detailsLabel: NSTextField!
  @IBOutlet var holdingLabel: NSTextField!

  override func viewDidLoad() {
    super.viewDidLoad()

  }

  @IBAction func clickDelete(_ sender: Any) {
    print("Delete")
  }
}

In the controller where I have the NSStackView and want to add these rows, I have tried something along these lines, but I'm mostly confused how this is supposed to happen. This doesn't work, but I'm unsure how to proceed:

let test = ["one", "two", "three"]
for label in test{
  let approachRowVC = self.storyboard?.instantiateController(withIdentifier: "ApproachRow") as! ApproachRowVC
  approachRowVC.quantityLabel.stringValue = label
  approachList.addArrangedSubview(approachRowVC.view)
}

Any recommendations on how to pull this off?

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128
  • Can you expand on what doesn't work about the provided sample? That should work unless `storyboard` is nil, no controller is found with that identifier, or if the resulting view has no constraints to define its size (and so get squashed to zero size in the stack view) – Taylor Jan 24 '17 at 20:02
  • I was getting a crash (nil value) when I was setting the `stringValue` on the `quantityLabel`. But after adding this right after the controller instantiation, it works: `let _ = approachRowVC.view`. I guess the views hadn't loaded yet as can happen with Mac apps, it seems. – Clifton Labrum Jan 24 '17 at 22:17
  • i think this is more like a code design question. btw it helped me a lot :) – EFE Jan 29 '17 at 06:23

0 Answers0