macos OS X - NSViewController 如何找到它的窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5711367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OS X - How can a NSViewController find its window?
提问by AJ.
I have a Document based core data app. The main document window has a number of views, each controlled by its own custom NSViewController
which are switched in as necessary. I want each of these view controllers to be able to drop down a custom modal sheet from the document window. However because the views are separate and not in the MyDocument
nib
I cannot link the view to the document window in IB. This means that when I call
我有一个基于文档的核心数据应用程序。主文档窗口有许多视图,每个视图都由自己的自定义控制NSViewController
,根据需要切换。我希望这些视图控制器中的每一个都能够从文档窗口下拉自定义模式表。但是,因为视图是分开的并且不在MyDocument
nib
IB 中,所以我无法将视图链接到文档窗口。这意味着当我打电话
[NSApp beginSheet: sheetWindow modalForWindow: mainWindow modalDelegate: self didEndSelector: @selector(didEndSheet:returnCode:contextInfo:) contextInfo: nil];
I'm supplying nil for mainWindow
and the sheet therefore appears detached.
我正在为 nil 提供mainWindow
,因此该表看起来是分离的。
Any suggestions?
有什么建议?
Many Thanks
非常感谢
回答by Tom Dalling
You can use [[self view] window]
您可以使用 [[self view] window]
回答by Tim Closs
Indeed, it's self.view.window
(Swift).
确实,它是self.view.window
(斯威夫特)。
This may be nil in viewDidLoad() and viewWillAppear(), but is set properly by the time you get to viewDidAppear().
这在 viewDidLoad() 和 viewWillAppear() 中可能为零,但在您到达 viewDidAppear() 时已正确设置。
回答by JeremyP
If your controller can get access to the NSDocumentsubclass, you can use -windowForSheet
如果您的控制器可以访问NSDocument子类,则可以使用-windowForSheet
回答by marcprux
One issue with the other answers (i.e., just looking at self.view.window
) is that they don't take into account the case that when a view is hidden, its window
property will be nil
. A view might be hidden for a lot of reasons (for example, it might be in one of the unselected views in a tab view).
其他答案(即,仅查看self.view.window
)的一个问题是,他们没有考虑到隐藏视图时其window
属性将为 的情况nil
。视图可能因多种原因被隐藏(例如,它可能位于选项卡视图中未选择的视图之一中)。
The following (swift) extension will provide the windowController
for a NSViewController
by ascending the view controller hierarchy, from which the window
property may then be examined:
以下(swift)扩展将通过提升视图控制器层次结构来提供windowController
for a NSViewController
,window
然后可以检查属性:
public extension NSViewController {
/// Returns the window controller associated with this view controller
var windowController: NSWindowController? {
return ((self.isViewLoaded == false ? nil : self.view)?.window?.windowController)
?? self.parent?.windowController // fallback to the parent; hidden views like those in NSTabView don't have a window
}
}
回答by user1105951
more about Tim Closs answer :
更多关于蒂姆·克洛斯的回答:
-(void)viewDidAppear
{
self.view.window.title = @"title-viewDidAppear"; //this only works when and after viewDidAppeer is called
}
-(void)viewWillDisappear
{
self.view.window.title = @"title-viewWillDisappear"; //this only works before and when viewWillDisappear is called
}