xcode 替换已弃用的 NSNibLoading 方法(loadNibFile:、loadNibNamed: 等)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12767838/
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-09-15 01:43:33  来源:igfitidea点击:

Replacements for the deprecated NSNibLoading methods (loadNibFile:, loadNibNamed:, etc.)?

objective-cxcodemacoscocoaappkit

提问by maseth

I have found that the NSNibLoading methods in NSBundle:

我发现 NSBundle 中的 NSNibLoading 方法:

+[NSBundle loadNibFile:externalNameTable:withZone:]
+[NSBundle loadNibNamed:owner:]
-[NSBundle loadNibFile:externalNameTable:withZone:]

have all been marked deprecated in 10.8 - what is the proper way to load the nibs in 10.8 and later?

在 10.8 中都被标记为弃用 - 在 10.8 及更高版本中加载笔尖的正确方法是什么?

I'm trying to create a custom sheet in my app, do I have to create NSWindowControllerwith initWithWindowNibNamefor the custom sheet?

我想创建我的应用程序自定义的纸张,我必须创建NSWindowController具有initWithWindowNibName自定义纸张?

采纳答案by Jay

The NSBundleclass method loadNibNamed:owner:is deprecated in OS X v10.8,
loadNibNamed:owner:topLevelObjects:is notand the comments in the documentationstate why:

NSBundle类方法loadNibNamed:owner:是在OS X v10.8过时,
loadNibNamed:owner:topLevelObjects:不是和意见的文件状态原因:

Unlike legacy methods, the objects adhere to the standard cocoa memory management rules; it is necessary to keep a strong reference to them by using IBOutlets or holding a reference to the array to prevent the nib contents from being deallocated.

与遗留方法不同,对象遵循标准的可可内存管理规则;有必要通过使用 IBOutlets 或持有对数组的引用来保持对它们的强引用,以防止 nib 内容被释放。

回答by Dave Higgins

If your app is going to support Lion, then loadNibNamed:owner:topLevelObjects:will not fire and you'll get an exception (unrecognized selector) when run on Lion. After some searching around I came up with this:

如果您的应用程序将支持 Lion,则loadNibNamed:owner:topLevelObjects:不会触发,并且在 Lion 上运行时您将收到异常(无法识别的选择器)。经过一番搜索,我想出了这个:

    // loadNibNamed:owner:topLevelObjects was introduced in 10.8 (Mountain Lion).
    // In order to support Lion and Mountain Lion +, we need to see which OS we're
    // on. We do this by testing to see if [NSBundle mainBundle] responds to
    // loadNibNamed:owner:topLevelObjects: ... If so, the app is running on at least
    // Mountain Lion... If not, then the app is running on Lion so we fall back to the
    // the older loadNibNamed:owner: method. If your app does not support Lion, then
    // you can go with strictly the newer one and not deal with the if/else conditional.

    if ([[NSBundle mainBundle] respondsToSelector:@selector(loadNibNamed:owner:topLevelObjects:)]) {
        // We're running on Mountain Lion or higher
        [[NSBundle mainBundle] loadNibNamed:@"NibName"
                                      owner:self
                            topLevelObjects:nil];
    } else {
        // We're running on Lion
        [NSBundle loadNibNamed:@"NibName"
                         owner:self];
    }

If you really want to use topLevelObjects:&arrayfor Mountain Lion +, and you also want to support Lion, it looks like you will need to fall back on loadNibFile:externalNameTable:withZone: (available as both a class and instance method) for the Lion condition (I could be wrong about this one). I'm getting the impression that loadNibNamed:owner:topLevelObjects:was created to replace this.

如果您真的想topLevelObjects:&array用于 Mountain Lion +,并且您也想支持 Lion,那么对于 Lion 条件 (我可能是错的)。我得到的印象loadNibNamed:owner:topLevelObjects:是为了取代它而创造的。

I've also read elsewhere that when using the newer loadNibNamed:owner:topLevelObjects:for a sheet that you should uncheck "Release When Closed" for the sheet (window). This should be taken care of when you close the sheet:

我还在其他地方读到过,当使用较新的loadNibNamed:owner:topLevelObjects:工作表时,您应该取消选中工作表(窗口)的“关闭时发布”。关闭工作表时应注意这一点:

[self.sheet close];
self.sheet = nil;

I'm not sure exactly what should be done about that checkbox if you're opening a non-modal window. Any ideas?

如果您要打开非模态窗口,我不确定应该对该复选框做些什么。有任何想法吗?