访问 NIB 窗口控件

发布于 2024-09-09 23:22:00 字数 539 浏览 4 评论 0原文

我从 NSWindowController 继承了一个新类来实现 windowDidLoad,然后访问 NIB 定义的窗口控件:

- ( void ) windowDidLoad
{
   NSArray * controls = [ [ [ self window ] contentView ] subviews ];
   int i;

   NSRunAlertPanel( @"windowDidLoad", @"", @"OK", NULL, NULL );

   if( [ controls count ] == 0 )
      NSRunAlertPanel( @"no hay controles", @"", @"OK", NULL, NULL );   

   for( i = 0; i < [ controls count ]; i++ )
      NSRunAlertPanel( @"control", @"", @"OK", NULL, NULL );   
}

代码执行正常。显示 NIB 窗口,但子视图没有元素。如何访问窗口子控件?谢谢,

I have inherited a new class from NSWindowController to implement windowDidLoad and then access to the NIB defined window controls:

- ( void ) windowDidLoad
{
   NSArray * controls = [ [ [ self window ] contentView ] subviews ];
   int i;

   NSRunAlertPanel( @"windowDidLoad", @"", @"OK", NULL, NULL );

   if( [ controls count ] == 0 )
      NSRunAlertPanel( @"no hay controles", @"", @"OK", NULL, NULL );   

   for( i = 0; i < [ controls count ]; i++ )
      NSRunAlertPanel( @"control", @"", @"OK", NULL, NULL );   
}

code execution goes fine. The NIB window is shown, but subviews has no elements. How to access the window child controls ? Thanks,

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

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

发布评论

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

评论(1

酒与心事 2024-09-16 23:22:00

显示了 NIB 窗口,但子视图没有元素。

您可能忘记将窗户插座连接到窗户上。当插座未连接时,插座属性保存nil,因此[self window]返回nil

然后您向nil发送消息。我说“消息”是因为 每条发送到 nil 的消息都不执行任何操作,并返回 nil00.0酌情。这意味着您将 contentView 消息发送到 nil,从而返回 nil,这意味着您发送 subviews 消息到nil,这样也返回nil

正如我所说,发送给 nil 的消息会返回 nil00.0;当您将 count 消息发送到 controls 时,由于 controlsnil(如上一段所述),该消息返回0

解决方法是在 IB 中打开笔尖并将控制器的 window 插座连接到窗口。

顺便说一下,你不应该使用索引来循环 NSArray。有一种更简单、更清晰的方法: 快速枚举

The NIB window is shown, but subviews has no elements.

You probably forgot to hook up the window outlet to your window. When the outlet is not hooked up, the outlet property holds nil, so [self window] returns nil.

Then you send messages to nil. I say “messages” because every message to nil does nothing and returns nil, 0, or 0.0 as appropriate. That means you send the contentView message to nil, so that returns nil, which means you send the subviews message to nil, so that also returns nil.

As I said, a message to nil returns nil, 0, or 0.0; when you send the count message to controls, since controls is nil as explained in the previous paragraph, that message returns 0.

The fix is to open your nib in IB and connect your controller's window outlet to your window.

By the way, you should not use indexes to loop over NSArrays. There is a simpler, cleaner way to do it: Fast Enumeration.

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