访问 NIB 窗口控件
我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能忘记将窗户插座连接到窗户上。当插座未连接时,插座属性保存
nil
,因此[self window]
返回nil
。然后您向
nil
发送消息。我说“消息”是因为 每条发送到nil
的消息都不执行任何操作,并返回nil
、0
或0.0酌情
。这意味着您将
contentView
消息发送到nil
,从而返回nil
,这意味着您发送subviews
消息到nil
,这样也返回nil
。正如我所说,发送给
nil
的消息会返回nil
、0
或0.0
;当您将count
消息发送到controls
时,由于controls
为nil
(如上一段所述),该消息返回0
。解决方法是在 IB 中打开笔尖并将控制器的
window
插座连接到窗口。顺便说一下,你不应该使用索引来循环 NSArray。有一种更简单、更清晰的方法: 快速枚举。
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]
returnsnil
.Then you send messages to
nil
. I say “messages” because every message tonil
does nothing and returnsnil
,0
, or0.0
as appropriate. That means you send thecontentView
message tonil
, so that returnsnil
, which means you send thesubviews
message tonil
, so that also returnsnil
.As I said, a message to
nil
returnsnil
,0
, or0.0
; when you send thecount
message tocontrols
, sincecontrols
isnil
as explained in the previous paragraph, that message returns0
.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.