मैं वर्तमान में एक विंडो नियंत्रक के साथ एक विंडो प्रदर्शित करने की कोशिश कर रहा हूँ। है यही कारण है कि मैं क्या है:self.window हमेशा शून्य
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 {खुद को वापस करें। विन्डो के रूप में! TweetWindow; }
यह गंभीर त्रुटि का कहना है: अप्रत्याशित रूप से, जबकि एक वैकल्पिक मूल्य unwrapping शून्य पाया, इसलिए खिड़की नहीं के बराबर है।
खिड़की शून्य क्यों है? क्या मैं मूल्य को बहुत जल्दी या कुछ तक पहुंचने की कोशिश कर रहा हूं?
यहाँ कुछ तस्वीरें हैं:
धन्यवाद।
मैंने ओवर्रिडेन फ़ंक्शन ("शोविंडो") हटा दिया लेकिन यह अभी भी मुझे "विंडोडिडलोड" में अपवाद फेंकता है। ऐसा लगता है कि आउटलेट असफल रहा है या ऐसा कुछ है। मैंने प्रतिनिधि को फाइल के मालिक से जोड़ा लेकिन कोई बदलाव नहीं। –
"मैंने प्रतिनिधि को फाइल के मालिक से जोड़ा" महत्वपूर्ण बात यह नहीं है कि खिड़की का प्रतिनिधि फ़ाइल के मालिक से जुड़ा हुआ है। यह है कि फ़ाइल के मालिक का विंडो आउटलेट विंडो से जुड़ा हुआ है। अन्य चीजों की जांच करने के लिए: विंडो में फ़ाइल के मालिक की कक्षा एनआईबी 'TweetWindowController' है, है ना? क्या आपने शायद एनआईबी में उस वर्ग का एक और उदाहरण तत्काल किया था? यही है, क्या आपने एनआईबी में "ऑब्जेक्ट" खींच लिया और अपनी कक्षा को 'TweetWindowController' पर सेट किया? यदि ऐसा है, तो एनआईबी से इसे हटा दें। –
मैंने इसे बदल दिया ताकि फ़ाइल के मालिक का आउटलेट प्रतिनिधि अब विंडो को संदर्भित कर सके। फ़ाइल के मालिक का वर्ग "TweetWindow" रहा है। मैंने TweetWindowController के साथ इसे आजमाया, लेकिन अपवाद अभी भी दिखाई देता है। लेकिन फिर भी यह समझ में नहीं आता है, है ना? मेरे पास कोई अन्य ऑब्जेक्ट नहीं है जिसने मेरे एनआईबी में कक्षा के रूप में "TweetWindowController" सेट किया है, इसलिए यह संभव नहीं है कि यह अपवाद का स्रोत है, मुझे लगता है। –