xibファイルを読み込んでNSWindowContrllerを表示する
概要
- xibファイルを読み込んで別ウィンドウを表示したい。
-
Storyboard
から、NSWindowController
(with xibファイル)を表示する
参考
実装
-
NSWindowController
のクラスを定義する。(このときxibファイルも一緒に作成する)
SampleWindowController.swift
import Cocoa
class SampleWindowController: NSWindowController {
// MARK: - Properties
let windowTitle: String
let message: String
@IBOutlet weak var messageLabel: NSTextField!
// MARK: - Lifecycle
init(withTitle title: String, message: String, window: NSWindow?) {
self.windowTitle = title
self.message = message
super.init(window: window)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func windowDidLoad() {
super.windowDidLoad()
configureUI()
}
override var windowNibName: NSNib.Name? {
return String(describing: type(of: self))
}
// MARK: - Helpers
private func configureUI() {
window?.title = windowTitle
messageLabel.stringValue = message
}
}
- クラスと
xib
の結びつけは下記のコード。これが今回のポイント。
SampleWindowController.swift
override var windowNibName: NSNib.Name? {
return String(describing: type(of: self))
}
-
ViewController
からの呼び出し方は以下の通り。
ViewController.swift
var sampleWindowController: SampleWindowController?
// (中略)
@IBAction func openingWindowButtonClicked(_ sender: Any) {
let sampleWindowController = SampleWindowController(withTitle: "Title of SampleWindowController",
message: "passed message for SampleWindowController..",
window: nil)
sampleWindowController.showWindow(self)
self.sampleWindowController = sampleWindowController
}
-
NSWindowController
のクラスを定義する。(このときxibファイルも一緒に作成する)
SampleWindowController.swift
import Cocoa
class SampleWindowController: NSWindowController {
// MARK: - Properties
let windowTitle: String
let message: String
@IBOutlet weak var messageLabel: NSTextField!
// MARK: - Lifecycle
init(withTitle title: String, message: String, window: NSWindow?) {
self.windowTitle = title
self.message = message
super.init(window: window)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func windowDidLoad() {
super.windowDidLoad()
configureUI()
}
override var windowNibName: NSNib.Name? {
return String(describing: type(of: self))
}
// MARK: - Helpers
private func configureUI() {
window?.title = windowTitle
messageLabel.stringValue = message
}
}
- クラスと
xib
の結びつけは下記のコード。これが今回のポイント。
SampleWindowController.swift
override var windowNibName: NSNib.Name? {
return String(describing: type(of: self))
}
-
ViewController
からの呼び出し方は以下の通り。
ViewController.swift
var sampleWindowController: SampleWindowController?
// (中略)
@IBAction func openingWindowButtonClicked(_ sender: Any) {
let sampleWindowController = SampleWindowController(withTitle: "Title of SampleWindowController",
message: "passed message for SampleWindowController..",
window: nil)
sampleWindowController.showWindow(self)
self.sampleWindowController = sampleWindowController
}
Author And Source
この問題について(xibファイルを読み込んでNSWindowContrllerを表示する), 我々は、より多くの情報をここで見つけました https://qiita.com/IKEH/items/48b012027ba5269060c2著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .