在可可中创建 NSApplication?
我已经搜索了很长一段时间,但根本找不到任何东西,我已经有了可以创建的窗口,但是现在当我尝试为它调用 makeKeyAndOrderFront 时,没有应用程序可以调用它,所以我是想知道是否有人可以提供一个关于如何创建应用程序的快速示例?我发现它是 NSApplication,我需要 setDelegate 等,但不确定到底需要什么。例如,要创建一个窗口,我需要以下代码:
NSWindow *window;
window = [[NSWindow alloc] initWithContentRect: NSMakeRect(10, 100, 150, 200)
styleMask:( NSResizableWindowMask | NSClosableWindowMask | NSTitledWindowMask)
backing: NSBackingStoreBuffered
defer: NO];
[window setTitle: @"Test window"];
那么任何人都可以给我相关代码,但是对于 NSApplication,我已经有按钮和窗口了?
I've been searching for quite a while now but simply can't find anything, I already have window which I can create, but now when I tried to call makeKeyAndOrderFront for it, there was no application to call it for, so I was wondering if anyone could give a quick example on how do I create an application? I found out that it's NSApplication and I need to setDelegate etc, but not sure what exactly is required. for example, to create a window, I would've needed this code:
NSWindow *window;
window = [[NSWindow alloc] initWithContentRect: NSMakeRect(10, 100, 150, 200)
styleMask:( NSResizableWindowMask | NSClosableWindowMask | NSTitledWindowMask)
backing: NSBackingStoreBuffered
defer: NO];
[window setTitle: @"Test window"];
So can anyone give me related code, but for NSApplication, I already have buttons and window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要完全在代码中完成此操作,根本不需要笔尖,您首先应该从 Info.plist 文件中删除
NSMainMenuNib
条目。然后,在main
中:这是最低限度。像往常一样,设置委托是可选的。
当您以通常的方式创建应用程序时,所有这些(除了您在 IB 中执行的创建和添加子视图之外)都会为您完成。
To do this entirely in code, with no nib at all, you first should remove the
NSMainMenuNib
entry from your Info.plist file. Then, inmain
:That's the bare minimum. Setting a delegate is, as usual, optional.
When you create an application the usual way, all of this—except creating and adding subviews, which you'd do in IB—is done for you.
只需从 Xcode 中提供的模板之一开始即可。然后
NSApplication
会自动设置。如果您想通过编码一切来艰难地做到这一点,本系列博客文章“无 Nibs 工作”将为您提供帮助,从这里开始。但除非您很好地掌握了 Cocoa 的内部工作原理,否则您不想这样做。
因此,只需遵循人们通常所做的事情,并从 Xcode 中提供的模板开始即可。
Just start from one of the template available in Xcode. Then
NSApplication
is automatically set up.If you want to do in the hard way, by coding everything, this series of blog posts "Working Without Nibs" will help you, which starts here. But you don't want to do that unless you have a good grasp of the inner workings of Cocoa.
So, just stick to what people usually do, and start from the template available in Xcode.