xcode 在 OS X 中打开和创建窗口

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

Open and create window in OS X

objective-cxcodemacoscocoanib

提问by Aldee

How can I open a nib file from a click event in Cocoa ? I have 2 nib files created; the mainmenu(default) and my customWindow. Now, what I want is when I click a certain button from the mainmenu nib file, it will trigger to open my customWindow. How can it be done?

如何从 Cocoa 中的单击事件打开 nib 文件?我创建了 2 个 nib 文件;主菜单(默认)和我的自定义窗口。现在,我想要的是当我从主菜单 nib 文件中单击某个按钮时,它会触发打开我的 customWindow。怎么做到呢?

回答by Devarshi

Try this code, if you are using window controller:

如果您使用的是窗口控制器,请尝试以下代码:

NSWindowController* yourWindowController = [[NSWindowController alloc] initWithWindowNibName:@"YourXIBName"];
[yourWindowController showWindow:self]

A generic way to do this is by using below code:

执行此操作的通用方法是使用以下代码:

loadNibNamed:owner:

loadNibNamed:所有者:

回答by zrslv

If you can put both of your windows into the same nib, you can control-drag your button to the window you want to open, select "makeKeyAndOrderFront", and then uncheck "Release When Closed" and "Visible At Launch" checkboxes in the Attributes inspector.

如果您可以将两个窗口放入同一个笔尖,您可以按住 Control 拖动按钮到要打开的窗口,选择“makeKeyAndOrderFront”,然后取消选中“关闭时释放”和“启动时可见”复选框属性检查器。

connect an action

连接一个动作

set the attributes

设置属性

But seriously, read View Programming Guide. It will make life much easier.

但说真的,请阅读查看编程指南。这将使生活变得更加轻松。

回答by Kyr Dunenkoff

Nibs are just visual representation of a view graph with connections to objects in your code. Start with the basics, read View Programming Guide.

Nib 只是视图图的可视化表示,其中包含与代码中对象的连接。从基础开始,阅读查看编程指南

回答by Daniel

You could also link the clicking of the menu button to an IBAction in your app-delegate, which calls

您还可以将菜单按钮的点击链接到您的应用程序委托中的 IBAction,它调用

[NSBundle loadNibNamed:"myNib" withDelegate:self];

I'm not sure if that's right, but check the docs for NSBundle.

我不确定这是否正确,但请查看 NSBundle 的文档。