Tôi hiện đang cố gắng hiển thị một cửa sổ với bộ điều khiển cửa sổ. Đó là những gì tôi có:self.window luôn luôn là nil
NSWindow lớp con
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 lớp con:
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)
}
}
Tất nhiên, tôi đã có một .xib-file, quá, có chứa cửa sổ của tôi. Nó được gọi là "TweetWindow.xib". Bây giờ, cho đến nay điều này sẽ là ok.
Trong AppDelegate.swift của tôi, tôi làm như sau:
@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);
}
}
Vấn đề của tôi là như sau: Khi tôi cố gắng nhấp chuột vào nút của tôi được liên kết với các IBAction xuống đó để hiển thị các cửa sổ, một ngoại lệ được ném ở đây: var tweetWindow: TweetWindow {return self.window as! TweetWindow; }
Nó nói lỗi nghiêm trọng: bất ngờ tìm thấy con số không trong khi unwrapping một giá trị bắt buộc, vì vậy cửa sổ là con số không.
Tại sao cửa sổ lại ở đó? Tôi đang cố gắng truy cập vào giá trị quá sớm hay gì đó?
Dưới đây là một số hình ảnh:
Cảm ơn.
Tôi đã xóa hàm overriden ("showWindow") nhưng nó vẫn ném cho tôi ngoại lệ trong "windowDidLoad". Dường như ổ cắm bị hỏng hoặc như thế. Tôi đã kết nối đại biểu với Chủ sở hữu tệp nhưng không có thay đổi nào. –
"Tôi đã kết nối đại biểu với Chủ sở hữu tệp" Điều quan trọng không phải là đại biểu của cửa sổ được kết nối với Chủ sở hữu tệp. Đó là ổ cắm cửa sổ của Chủ sở hữu tệp được kết nối với cửa sổ. Những thứ khác cần kiểm tra: lớp của Chủ sở hữu tập tin trong cửa sổ NIB là 'TweetWindowController', đúng không? Có phải bạn có thể khởi tạo một thể hiện khác của lớp đó trong NIB? Tức là, bạn đã kéo một "đối tượng" vào NIB và đặt lớp của nó thành 'TweetWindowController'? Nếu vậy, hãy xóa nó khỏi NIB. –
Tôi đã thay đổi điều đó để đại diện cửa hàng của Chủ sở hữu tệp bây giờ đề cập đến cửa sổ. Lớp Chủ sở hữu tệp đã là "TweetWindow". Tôi đã thử nó với TweetWindowController, nhưng ngoại lệ vẫn xuất hiện. Nhưng điều đó sẽ không có ý nghĩa thì sao? Tôi không có các đối tượng khác đã đặt "TweetWindowController" làm lớp trong NIB của tôi, vì vậy không thể đây là nguồn gốc của ngoại lệ, tôi nghĩ vậy. –