在可可中创建 NSApplication?

发布于 2024-11-15 17:29:39 字数 539 浏览 3 评论 0原文

我已经搜索了很长一段时间,但根本找不到任何东西,我已经有了可以创建的窗口,但是现在当我尝试为它调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-11-22 17:29:39

要完全在代码中完成此操作,根本不需要笔尖,您首先应该从 Info.plist 文件中删除 NSMainMenuNib 条目。然后,在 main 中:

  1. 创建一个自动释放池。
  2. 向 NSApplication 类询问共享应用程序对象。如果之前没有任何东西请求过共享应用程序对象,这将创建一个。
  3. 创建一个 NSMenu 来保存主菜单。为每个标准菜单创建一个菜单项,然后为每个标准菜单创建一个菜单,并将每个菜单设置为该菜单项的子菜单。对于每个菜单,创建应包含在其中的项目。不要忘记本地化标题并设置适当的等效键。将顶层菜单设置为应用程序的主菜单。
  4. 对于具有单个窗口的应用程序,此时创建窗口,创建并为其提供内容视图,创建您想要的任何视图并将它们添加到内容视图中,然后对窗口进行排序。
  5. 告诉应用程序运行。
  6. 将水池排干。
  7. 成功退出。

这是最低限度。像往常一样,设置委托是可选的。

当您以通常的方式创建应用程序时,所有这些(除了您在 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, in main:

  1. Create an autorelease pool.
  2. Ask the NSApplication class for the shared application object. If nothing has ever asked for the shared application object before, this will create one.
  3. Create an NSMenu to hold the main menu. Create a menu item for each of the standard menus, then create a menu for each of the standard menus, and set each menu as the menu item's submenu. For each menu, create the items that should go in it. Don't forget to localize titles and to set appropriate key equivalents. Set the top-level menu as the application's main menu.
  4. For an application with a single window, create the window at this point, create and give it its content view, create any views you want in it and add them to the content view, and order the window in.
  5. Tell the application to run.
  6. Drain the pool.
  7. Exit successfully.

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.

时光病人 2024-11-22 17:29:39

只需从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文