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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:58:02  来源:igfitidea点击:

OS X - How can a NSViewController find its window?

macoscocoacocoa-sheet

提问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 NSViewControllerwhich 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 MyDocumentnibI cannot link the view to the document window in IB. This means that when I call

我有一个基于文档的核心数据应用程序。主文档窗口有许多视图,每个视图都由自己的自定义控制NSViewController,根据需要切换。我希望这些视图控制器中的每一个都能够从文档窗口下拉自定义模式表。但是,因为视图是分开的并且不在MyDocumentnibIB 中,所以我无法将视图链接到文档窗口。这意味着当我打电话

[NSApp beginSheet: sheetWindow modalForWindow: mainWindow modalDelegate: self didEndSelector: @selector(didEndSheet:returnCode:contextInfo:) contextInfo: nil];

I'm supplying nil for mainWindowand 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 windowproperty 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 windowControllerfor a NSViewControllerby ascending the view controller hierarchy, from which the windowproperty may then be examined:

以下(swift)扩展将通过提升视图控制器层次结构来提供windowControllerfor a NSViewControllerwindow然后可以检查属性:

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
}