私はObjective-Cでこのタイプのカスタムセグをしましたが、それは簡単ではありません。制約の権利が悪いAppleがビューコントローラに必ずしも自分のウィンドウが必要というわけではないと分かるまで、コンテナビューを使うことをお勧めします。
この例では、コンテナビューでViewController
を設定し、ストーリーボードのFirstContained
ビューコントローラにリンクしています。 「次へ」ボタンがあります。
@IBAction func goToNext(sender: NSButton) {
NSNotificationCenter.defaultCenter().postNotificationName(ViewController.SecondController, object: nil)
}
私は、ストーリーボードにSecondContained
ビューコントローラを作成し、それに識別子を与える「second_contained」。それは "戻る"ボタンを持っています。
@IBAction func goBack(sender: NSButton) {
NSNotificationCenter.defaultCenter().postNotificationName(ViewController.FirstController, object: nil)
}
ViewController
トランジションのすべての作業を行います。 (制約を得ることは右、まだいくつかの努力を要することに注意してください。SecondContained
の見解で圧縮抵抗を減らすことによって開始します。)
import Cocoa
class ViewController: NSViewController {
static let FirstController = "FirstController"
static let SecondController = "SecondController"
@IBOutlet weak var container: NSView!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "firstSelected:", name: ViewController.FirstController, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "secondSelected:", name: ViewController.SecondController, object: nil)
// This puts the "SecondContained" controller at location zero in the childViewControllers array.
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateControllerWithIdentifier("second_contained") as? SecondContained
if let second = controller {
addChildViewController(second)
}
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func removePreviousView() {
if let oldView: NSView = container.subviews[0] {
oldView.removeFromSuperview()
} else {
print("No previous view found")
}
}
// This is a hack.
// It would be better to search for the controller by a reliable identifier rather than a number.
func useController(offset: Int) {
guard childViewControllers.count > offset else {
print("Bad offset \(offset) for \(childViewControllers.count)-long array")
return
}
if let controller: NSViewController = childViewControllers[offset] {
container.addSubview(controller.view)
} else {
print("No view controller!?")
}
}
func firstSelected(notification: NSNotification) {
removePreviousView()
useController(1)
}
func secondSelected(notification: NSNotification) {
removePreviousView()
useController(0)
}
}
注これはスウィフト2.1であること。ロジックは移植可能でなければなりませんが、構文が変更されたかどうかはわかりません。