在AppDelegate中丢失对NSWindows的引用 [英] Losing reference to NSWindows in AppDelegate
问题描述
我遇到了一些问题,在这里我失去了在AppDelegate中声明的NSWindows的参考, class AppDelegate:NSObject,NSApplicationDelegate {
var window1:CustomWindow1!
var window2:CustomWindow2!
func setupWindows(){
self.window1 = CustomWindow1()
self.window2 = CustomWindow2()
let vc2 = VIEWOFSOMEVIEWCONTROLLER()
self .window2.contentView.addSubview(vc2.view)
self.window1.grandchildVC = vc2
self.window1.addChildWindow(self.window2 !, ordered:NSWindowOrderingMode.Above)
//等。
}
@IBAction addWindowsAgain(sender:AnyObject){
//这是失败的地方
if self.window1!= nil {
self.window1 == nil
}
}
}
class CustomWindow1(){
var grandchildVC:NSViewController
func deleteChildWindowThenSelf {
self.grandchildVC.view.window.close()
self.close()
}
}
我可以调用 deleteChildWindowThenSelf()
,并按预期摆脱窗口,但调用 addWindowsAgain
在AppDelegate中,如果失败与 lldb
错误。当调用 close
时,AppDelegate会丢失那些窗口变量吗?
NSWindow
有一个变量 releasedWhenClosed
,它是 true $ c $
将变量设置为 false
。
I'm running into issues where I lose reference to NSWindows that are declared in AppDelegate, with the code below.
class AppDelegate: NSObject, NSApplicationDelegate {
var window1: CustomWindow1!
var window2: CustomWindow2!
func setupWindows() {
self.window1 = CustomWindow1()
self.window2 = CustomWindow2()
let vc2 = VIEWOFSOMEVIEWCONTROLLER()
self.window2.contentView.addSubview(vc2.view)
self.window1.grandchildVC = vc2
self.window1.addChildWindow(self.window2!, ordered: NSWindowOrderingMode.Above)
// etc.
}
@IBAction addWindowsAgain(sender: AnyObject) {
// This is where if fails
if self.window1 != nil {
self.window1 == nil
}
}
}
class CustomWindow1() {
var grandchildVC: NSViewController
func deleteChildWindowThenSelf() {
self.grandchildVC.view.window.close()
self.close()
}
}
I am able to call deleteChildWindowThenSelf()
, and get rid of the windows as expected, but when calling addWindowsAgain
in AppDelegate, if fails with lldb
error. Does AppDelegate lose those window vars when close
is called, or am I misunderstanding something?
NSWindow
has a variable releasedWhenClosed
which is true
by default for custom created windows unless the window is owned by a window controller.
Set the variable to false
.
这篇关于在AppDelegate中丢失对NSWindows的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!