Skip to content

Instantly share code, notes, and snippets.

@danielctull
Created March 7, 2017 10:15
Show Gist options
  • Save danielctull/9ac923daa9751724a538fd608e39e154 to your computer and use it in GitHub Desktop.
Save danielctull/9ac923daa9751724a538fd608e39e154 to your computer and use it in GitHub Desktop.
A simple NSStoryboardSegue to replace the last view controller in a NSSplitViewController.
import Cocoa
class ReplaceSegue: NSStoryboardSegue {
override func perform() {
guard
let source = sourceController as? NSViewController,
let new = destinationController as? NSViewController,
let split = source.parent as? NSSplitViewController,
// Assuming that the last view controller in the split is
// the one that should be replaced, mimicking an iOS
// two-pane split view controller.
let old = split.childViewControllers.last,
let oldItem = split.splitViewItem(for: old)
else {
return
}
// Make sure we're adding a new item, it's possbile the
// split view only has one item – the source view controller!
if old != source {
split.removeSplitViewItem(oldItem)
}
// Create the new split item from the view controller
// and add it. This is all a bit legacy workaround.
let newItem = NSSplitViewItem(viewController: new)
split.addSplitViewItem(newItem)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment