gpt4 book ai didi

objective-c - 替换已弃用的 NSNibLoading 方法(loadNibFile :, loadNibNamed :, 等)?

转载 作者:太空狗 更新时间:2023-10-30 03:18:04 26 4
gpt4 key购买 nike

我发现 NSBundle 中的 NSNibLoading 方法:

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

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

我正在尝试在我的应用程序中创建自定义工作表,我是否必须为自定义工作表创建带有 initWithWindowNibNameNSWindowController

最佳答案

如果您的应用程序要支持 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];
}

如果你真的想为 Mountain Lion + 使用 topLevelObjects:&array,并且你还想支持 Lion,看起来你需要退回到 loadNibFile:externalNameTable:withZone:(可用作为类和实例方法)用于 Lion 条件(我对这个可能是错误的)。我的印象是创建了 loadNibNamed:owner:topLevelObjects: 来替换它。

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

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

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

关于objective-c - 替换已弃用的 NSNibLoading 方法(loadNibFile :, loadNibNamed :, 等)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12767838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com