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

问题描述 投票:0回答:1

我正在使用模式表(从顶部向下滑动)以获取用户输入。我目前有2个,除了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(_ :) 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”

NewItemSheetControllerNewRecurrenceItemSheetController类是NSWindowController的子类,仅与NSNib不同。名称和与不同UI相关的属性。据我所知,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)
   }
}

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

swift cocoa appkit
1个回答
0
投票

在Interface Builder中,需要通过窗口出口和委托将XIB窗口附加到文件的所有者。谢谢@Willeke。

© www.soinside.com 2019 - 2024. All rights reserved.