gpt4 book ai didi

cocoa - 访问 NIB 窗口控件

转载 作者:行者123 更新时间:2023-12-03 18:04:41 26 4
gpt4 key购买 nike

我从 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 窗口,但 subview 没有元素。如何访问窗口子控件?谢谢,

最佳答案

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

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

然后您向nil发送消息。我说“消息”是因为every message to nil does nothing and returns nil, 0, or 0.0 as appropriate 。这意味着您将 contentView 消息发送到 nil,从而返回 nil,这意味着您发送 subviews 消息到 nil,这样也返回 nil

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

解决方法是在 IB 中打开 Nib 并将 Controller 的 window socket 连接到您的窗口。

顺便说一下,你不应该使用索引来循环 NSArray。有一种更简单、更干净的方法:Fast Enumeration .

关于cocoa - 访问 NIB 窗口控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3273922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com