[英]Custom NSView with NSStackView
我正在尝试使用始终相同的自定义视图来实现NSStackView
。 所以我定义了我的 xib 文件,链接了我的类中的所有内容,关联了类,...但我确定我错过了一些东西,因为视图中的内容没有出现。 非常感谢您的帮助。
我的文件是这样的:
class CustomView: NSView, NSTableViewDelegate, NSTableViewDataSource {
@IBOutlet weak var segmentControlOutlet: NSSegmentedControl!
@IBOutlet weak var tableViewOutlet: NSTableView!
}
class ViewController: NSViewController, NSStackViewDelegate {
@IBOutlet weak var stackViewOutlet: NSStackView!
override func viewDidLoad() {
super.viewDidLoad()
stackViewOutlet.delegate = self
let newView = CustomView()
stackViewOutlet.addArrangedSubview(newView)
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
我找到了答案,您需要正确启动视图。 这样做:
class CustomView: NSView, NSTableViewDelegate, NSTableViewDataSource {
@IBOutlet var viewOutlet: NSView!
@IBOutlet weak var segmentControlOutlet: NSSegmentedControl!
@IBOutlet weak var tableViewOutlet: NSTableView!
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
Bundle.main.loadNibNamed("CustomView", owner: self, topLevelObjects: nil)
addSubview(viewOutlet)
viewOutlet.frame = self.bounds
viewOutlet.autoresizingMask = [.height, .width]
}
required init?(coder: NSCoder) {
super.init(coder: coder)
Bundle.main.loadNibNamed("CustomView", owner: self, topLevelObjects: nil)
addSubview(viewOutlet)
viewOutlet.frame = self.bounds
viewOutlet.autoresizingMask = [.height, .width]
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
}
请注意,当您加载笔尖名称时,“CustomView”是您的 xib 文件的名称;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.