我正在使用 NSSplitViewController 子类,并且没有调用委托方法。
这纯粹是在没有 nib/storyboard 的情况下以编程方式进行的。
可以将代码复制到新项目中。该文件需要命名为 main.swift。还要删除项目设置中的“主界面”。
// File: main.swift
import Cocoa
// AppDelegate
class AppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 600, height: 400),
styleMask: [ .titled, .closable, .resizable ],
backing: .buffered,
defer: false
)
func applicationDidFinishLaunching(_ aNotification: Notification) {
let splitViewController = MySplitViewController()
window.contentView = splitViewController.view
window.makeKeyAndOrderFront(nil)
}
}
// NSSplitViewController
class MySplitViewController: NSSplitViewController {
convenience init() {
self.init(nibName: nil, bundle: nil)
// Left
let viewController1 = NSViewController()
viewController1.view = NSView()
let item1 = NSSplitViewItem(viewController: viewController1)
item1.minimumThickness = 100
item1.maximumThickness = 200
addSplitViewItem(item1)
// Right
let viewController2 = NSViewController()
viewController2.view = NSView()
let item2 = NSSplitViewItem(viewController: viewController2)
addSplitViewItem(item2)
}
override func viewDidLoad() {
super.viewDidLoad()
print(splitView.delegate!) // Logs "Project1.MySplitViewController"
}
// Never called
override func splitView(_ splitView: NSSplitView, additionalEffectiveRectOfDividerAt dividerIndex: Int) -> NSRect {
print("\(#function)")
return super.splitView(splitView, additionalEffectiveRectOfDividerAt: dividerIndex)
}
}
let application = NSApplication.shared
let applicationDelegate = AppDelegate()
application.delegate = applicationDelegate
application.run()
运行代码时,您可以看到拆分视图显示并且工作正常。
viewDidLoad() 方法打印“Project1.MySplitViewController”,因此设置了委托。
但是没有调用 splitView(_:additionalEffectiveRectOfDividerAt:) 方法(或任何其他 NSSplitViewDelegate,如果已实现)。