使用 NSWindowController 操作窗口

发布于 2024-08-16 00:29:07 字数 239 浏览 6 评论 0原文

我有以下代码:

AXWindowController *controller = [[AXWindowController alloc] 
initWithWindowNibName:@"ActivateWindow"];
[controller showWindow:nil];

如何使用控制器对窗口 ActivateWindow 进行更改(例如更改窗口上的文本等)?逐步说明将不胜感激。谢谢!

I have the following code:

AXWindowController *controller = [[AXWindowController alloc] 
initWithWindowNibName:@"ActivateWindow"];
[controller showWindow:nil];

How do I use controller to make changes to the window ActivateWindow (eg. changing text on the window, etc.)? Step by step instructions would be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

瀟灑尐姊 2024-08-23 00:29:07

好吧,您的代码片段中没有处理任何实际的窗口元素,您的代码只是初始化并显示窗口。 (我刚刚快速浏览了一下,所以我假设它可以工作)

例如,如果您想在窗口中显示文本,最简单的方法是使用 NSTextField

非常简单的说明:

  1. 将“标签”项从 Interface Builder 库拖放到窗口中。
  2. 将按钮项从 Interface Builder 库拖放到窗口中。
  3. 在 XCode 中,在您的窗口控制器中,为您的标签创建一个 IBOutlet,例如 messageLabel
  4. 在 XCode 中,在您的窗口控制器中,为您的标签创建一个 IBAction,例如,changeLabel
  5. 在 Interface Builder 中,将一个“对象”拖放到您的文档中。 (快捷键 = CMD + 0)
  6. 在 Identity 选项卡下,将类更改为 AXWindowController。 (快捷键=CMD+6)按住
  7. Ctrl键并从对象拖动到标签上,选择出口messageLabel。
  8. 按住 Ctrl 键并从按钮拖动到对象,选择changeLabel。

现在,要在按下按钮时显示文本,您需要向changeLabel IBAction 添加代码来执行此操作。
例如 [messageLabel setTitleWithMnemonic:@"blah"];

最后,如果你希望它自动显示文本,你不妨在 Interface Builder 中更改标签内容/将代码放在 windowDidLoad 方法中在你的控制器中。

对我来说,这几乎是最简单的方法。不过,我建议您在深入研究更难的东西之前先阅读一些有关 Cocoa 和 Objective-C 的书籍/教程。

Well, none of the actual window elements are handled in your piece of code, your code just initializes and shows the window. (I've just had a quick scan through, so I'm assuming that it works)

If you want to, for example, display text in a window, the simplest way is to use an NSTextField.

Very simple instructions:

  1. Drag and drop a Label item from the Interface Builder library into your window.
  2. Drag and drop a button item from the Interface Builder library into your window.
  3. In XCode, in your window controller, create an IBOutlet for your label, e.g. messageLabel
  4. In XCode, in your window controller, create an IBAction for your label, e.g. changeLabel
  5. In Interface Builder, drag and drop an "object" into your document. (Shortcut = CMD + 0)
  6. Under the Identity tab, change the class to AXWindowController. (Shortcut = CMD + 6)
  7. Ctrl + drag from the object to the label, choose the outlet messageLabel.
  8. Ctrl + drag from the button to the object, choose changeLabel.

Now, to display text when you press the button, you would add code to the changeLabel IBAction to do so.
e.g. [messageLabel setTitleWithMnemonic:@"blah"];

And Finally, if you want it to automatically display text, you might as well just change the label content in Interface Builder / place the code in the windowDidLoad method in your controller.

That to me is pretty much the simplest way for you to do it. However, I recommend you read some books / tutorials on Cocoa and Objective-C before delving into harder stuff.

掩耳倾听 2024-08-23 00:29:07

当然,您可以在窗口控制器的代码中以编程方式对窗口进行更改。打开 AXWindowController.m 并执行您想做的任何操作。如果不确切知道您想要做什么(如果您只想创建界面,请使用 Interface Builder)或您的经验水平是什么,则很难给出更明确的答案。

You'd programmatically make changes to your window in the code for your window controller, of course. Open AXWindowController.m and do whatever it is you're wanting to do. It's hard to give a more definite answer without knowing exactly what you're trying to do (if you just want to create the interface, use Interface Builder) or what your experience level is.

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