gpt4 book ai didi

macos - self.window 总是 nil

转载 作者:IT王子 更新时间:2023-10-29 05:52:02 26 4
gpt4 key购买 nike

我目前正在尝试显示一个带有窗口 Controller 的窗口。这就是我所拥有的:

NSWindow 子类

import Cocoa
import CoreLocation

class TweetWindow: NSWindow {

var locationManager: CLLocationManager!
var geoCoder: CLGeocoder!

@IBAction func tweetButtonPressed(sender:NSButton) {

}

func initialize() {
self.titleVisibility = NSWindowTitleVisibility.Hidden;
self.locationManager = CLLocationManager();
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.distanceFilter = 10;

self.geoCoder = CLGeocoder();
}

func windowWillShow() {
if !self.visible {
let systemAppearanceName = (NSUserDefaults.standardUserDefaults().stringForKey("AppleInterfaceStyle") ?? "Light").lowercaseString;
let systemAppearance = systemAppearanceName == "dark" ? NSAppearance(named: NSAppearanceNameVibrantDark) : NSAppearance(named: NSAppearanceNameVibrantLight);
self.appearance = systemAppearance;

self.locationManager.startUpdatingLocation();
}
}

func windowWillClose() {
self.locationManager.stopUpdatingLocation();
}
}

NSWindowController 子类:

import Cocoa

class TweetWindowController: NSWindowController {

var tweetWindow: TweetWindow { return self.window as! TweetWindow; }

override func windowDidLoad() {
super.windowDidLoad()
self.tweetWindow.initialize()
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

override func showWindow(sender: AnyObject?) {
self.tweetWindow.windowWillShow()
super.showWindow(sender)
}
}

当然,我也有一个 .xib 文件,它包含我的窗口。它被称为“TweetWindow.xib”。现在,到目前为止这应该没问题。

在我的 AppDelegate.swift 中,我执行以下操作:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var tweetWindowController:TweetWindowController!;

func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
tweetWindowController = TweetWindowController(windowNibName: "TweetWindow");
}

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}

func showTweetWindowInternal() {
NSApp.activateIgnoringOtherApps(true);
tweetWindowController.showWindow(nil);
}

@IBAction func showTweetWindow(sender: AnyObject) {
showTweetWindowInternal();
}

@IBAction func quitApp(sender: AnyObject) {
NSApplication.sharedApplication().terminate(self);
}
}

我的问题如下:当我尝试单击与下方的 IBAction 关联的按钮以显示窗口时,此处抛出异常:var tweetWindow: TweetWindow { 将 self.window 返回为!推文窗口;

它说 fatal error :在展开可选值时意外发现 nil,因此 window 为 nil。

为什么 window nil 在那里?我是否试图过早访问该值?

这是一些照片:

Window

File's Owner

File's Owner Identity Inspector

谢谢。

最佳答案

初始化 NSWindowController 或子类的实例不会加载 NIB。在访问 window 属性或调用 showWindow() 方法(基本上是间接访问 window 属性)之前,不会加载 NIB。在您的情况下,由于您要重写 showWindow(),因此知道在调用父类(super class)实现之前不会加载 NIB 很重要。

所以,是的,在调用 super 之前,您在 showWindow() 覆盖中调用 self.tweetWindow.windowWillShow() 太早了。此时 NIB 尚未加载,因此 window socket 尚未连接到任何东西。

当然,您必须确保 NIB 中的 socket 确实已连接,否则它永远不会连接,即使在 NIB 加载后也是如此。但是在加载之前尝试访问它是第一个问题。

我认为您的 windowWillShow() 方法被误导了,至少在实现时是这样。窗口可以通过多种方式显示,而不仅仅是通过窗口 Controller 的 showWindow() 方法。例如,窗口和窗口 Controller 之外的某些东西可以执行 tweetWindowController.window.makeKeyAndOrderFront(nil)。如果你真的想做这样的事情,让窗口类覆盖各种 order...() 方法来查看窗口是否是第一次被订购,如果是,调用你的方法。


更新:

您的 NIB 中有几项配置错误。以下是修复它们所需要做的事情:

  • 断开当前从文件所有者的“委托(delegate)”导出到窗口的连接。单击您发布的任一屏幕截图中的“x”按钮。
  • 更改文件所有者的类别。当前是 TweetWindow。它应该是 TweetWindowController。 Controller 负责加载和拥有 NIB,因此文件所有者类应该是 Controller 类。
  • 将文件所有者的“窗口”导出连接到窗口。
  • 将窗口的“委托(delegate)”导出连接到文件所有者。

关于macos - self.window 总是 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31924887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com