【问题标题】:Why would NSWindowController return nil-value window property?为什么 NSWindowController 会返回零值窗口属性?
【发布时间】:2020-03-03 22:23:47
【问题描述】:

我正在使用模式表(从顶部向下滑动)来获取用户输入。我目前有 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.Name 和与不同 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)
   }
}

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

【问题讨论】:

  • window 插座是否连接在 xib 中?
  • 不。不是。 IB中也没有代表。谢谢!在 UIKit 世界中学习 AppKit 并不容易。

标签: swift cocoa appkit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多