Я использую модальные листы (сдвиньте сверху вниз), чтобы получить пользовательский ввод. В настоящее время у меня есть 2, которые я считаю идентичными, за исключением пользовательского интерфейса, каждый из которых представляет собой пару NIB + NSWindowController-subclass. Один работает как ожидалось, связывая входные данные с контроллером массива и табличным представлением. При попытке использовать другое свойство window
NSWindowController имеет значение 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 (_ :) Optional () "
Этот код не:
@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 "
Классы NewItemSheetController
и NewRecurrenceItemSheetController
являются подклассами NSWindowController и отличаются только NSNib.Name и свойствами, связанными с отличающимся пользовательским интерфейсом. Насколько я вижу, XIB и кнопки "подключены" аналогично. 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 с нулевым свойством окна?