为什么NSWindowController会返回零值窗口属性



我使用模式表(从顶部向下滑动(来获取用户输入。我目前有两个,除了UI之外,我认为是相同的,每个都是NIB+NSWindowController子类对。一种按预期工作,将输入绑定到数组控制器和表视图。尝试使用另一个时,NSWindowController的window属性为nil

此代码有效:

@IBAction func addItemButtonClicked(_ button: NSButton) {
let window = document?.windowForSheet
let windowController = NewItemSheetController()
windowController.typeChoices = newItemSheetTypeChoices
windowController.windowTitle = newItemSheetTitle
print(#function, windowController.window) // output below
window?.beginSheet(windowController.window!, completionHandler: { response in
// The sheet has finished. Did user click OK?
if response == NSApplication.ModalResponse.OK {
let structure = (self.newItemSheetController?.structure)!
self.document?.dataSource.structures.append(structure)
}
// All done with window controller.
self.newItemSheetController = nil
})
newItemSheetController = windowController
}

打印语句的输出:"addItemButtonClicked(_:(Option((">

这个代码没有:

@IBAction func addItemButtonClicked(_ button: NSButton) {
let window = document?.windowForSheet
let windowController = NewRecurrenceItemSheetController()
windowController.windowTitle = newItemSheetTitle
print(#function, windowController.window)
window?.beginSheet(windowController.window!, completionHandler: { response in
// The sheet has finished. Did user click OK?
if response == NSApplication.ModalResponse.OK {
let recurrence = (self.newItemSheetController?.recurrence)!
self.document?.dataSource.recurrences.append(recurrence)
}
// All done with window controller.
self.newItemSheetController = nil
})
newItemSheetController = windowController
}

打印语句的输出:"addItemButtonClicked(_:(nil">

NewItemSheetControllerNewRecurrenceItemSheetController是NSWindowController的子类,仅与NSNib不同。名称和属性与不同的UI相关。据我所见,XIB和Buttons是类似的"连线"。XIB使用相应的文件所有者。窗口对象具有默认类。

@objcMembers
class NewItemSheetController: NSWindowController {
/// other properties here
dynamic var windowTitle: String = "Add New Item"
override var windowNibName: NSNib.Name? {
return NSNib.Name(stringLiteral: "NewItemSheetController")
}
override func windowDidLoad() {
super.windowDidLoad()
titleLabel.stringValue = windowTitle
}
// MARK: - Outlets
@IBOutlet weak var titleLabel: NSTextField!
@IBOutlet weak var typeChooser: NSPopUpButton!

// MARK: - Actions
@IBAction func okayButtonClicked(_ sender: NSButton) {
window?.endEditing(for: nil)
dismiss(with: NSApplication.ModalResponse.OK)
}
@IBAction func cancelButtonClicked(_ sender: NSButton) {
dismiss(with: NSApplication.ModalResponse.cancel)
}
func dismiss(with response: NSApplication.ModalResponse) {
window?.sheetParent?.endSheet(window!, returnCode: response)
}
}

为什么一个返回会实例化一个具有零值窗口属性的windowController对象?

在接口生成器中,XIB窗口需要通过窗口出口和委托附加到文件的所有者。谢谢@Willeke。

最新更新