|
| 1 | +/* |
| 2 | +See LICENSE folder for this sample’s licensing information. |
| 3 | + |
| 4 | +Abstract: |
| 5 | +This sample's detail view controller showing the accessibility examples. |
| 6 | +*/ |
| 7 | + |
| 8 | +import Cocoa |
| 9 | +import MediaLibrary |
| 10 | + |
| 11 | +class DetailViewController: NSViewController { |
| 12 | + |
| 13 | + @IBOutlet var descriptionField: NSTextField! |
| 14 | + @IBOutlet var exampleArea: NSView! |
| 15 | + |
| 16 | + var detailItemRecord: Example! { |
| 17 | + didSet { |
| 18 | + // Remove the old child view controller, if any exists. |
| 19 | + if !childViewControllers.isEmpty { |
| 20 | + let vc = childViewControllers[0] |
| 21 | + vc.view.isHidden = true |
| 22 | + vc.removeFromParentViewController() |
| 23 | + } |
| 24 | + |
| 25 | + descriptionField.stringValue = "" |
| 26 | + |
| 27 | + guard detailItemRecord != nil else { return } |
| 28 | + |
| 29 | + // Update the description of the example. |
| 30 | + descriptionField.stringValue = detailItemRecord.desc |
| 31 | + |
| 32 | + // Check if this sample actually has a valid view controller to display. |
| 33 | + guard !detailItemRecord.viewControllerIdentifier.characters.isEmpty else { return } |
| 34 | + |
| 35 | + // Load the example storyboard and embed. |
| 36 | + let storyboard: NSStoryboard = |
| 37 | + NSStoryboard(name: NSStoryboard.Name(rawValue: detailItemRecord.viewControllerIdentifier), bundle: nil) |
| 38 | + |
| 39 | + let sceneIdentifier = NSStoryboard.SceneIdentifier(rawValue: detailItemRecord.viewControllerIdentifier) |
| 40 | + |
| 41 | + guard let buttonViewController = |
| 42 | + storyboard.instantiateController(withIdentifier: sceneIdentifier) as? NSViewController else { return } |
| 43 | + |
| 44 | + insertChildViewController(buttonViewController, at: 0) |
| 45 | + |
| 46 | + buttonViewController.view.translatesAutoresizingMaskIntoConstraints = false |
| 47 | + |
| 48 | + view.addSubview(buttonViewController.view) |
| 49 | + |
| 50 | + // Add the proper constraints to the detail view controller so it embeds properly with it's parent view controller. |
| 51 | + let top = NSLayoutConstraint(item: buttonViewController.view, |
| 52 | + attribute: .top, |
| 53 | + relatedBy: .equal, |
| 54 | + toItem: exampleArea, |
| 55 | + attribute: .top, |
| 56 | + multiplier: 1, |
| 57 | + constant: 0) |
| 58 | + let left = NSLayoutConstraint(item: buttonViewController.view, |
| 59 | + attribute: .left, |
| 60 | + relatedBy: .equal, |
| 61 | + toItem: exampleArea, |
| 62 | + attribute: .left, |
| 63 | + multiplier: 1, |
| 64 | + constant: 0) |
| 65 | + let height = NSLayoutConstraint(item: buttonViewController.view, |
| 66 | + attribute: .height, |
| 67 | + relatedBy: .equal, |
| 68 | + toItem: exampleArea, |
| 69 | + attribute: .height, |
| 70 | + multiplier: 1, |
| 71 | + constant: 0) |
| 72 | + let width = NSLayoutConstraint(item: buttonViewController.view, |
| 73 | + attribute: .width, |
| 74 | + relatedBy: .equal, |
| 75 | + toItem: exampleArea, |
| 76 | + attribute: .width, |
| 77 | + multiplier: 1, |
| 78 | + constant: 0) |
| 79 | + view.addConstraints([top, left, height, width]) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | + |
0 commit comments