简体   繁体   English

通过可可中的标签号获取NSTextfield的值

[英]Get value of NSTextfield by tag number in Cocoa

I have a problem : How can i do If i want get value of NSTextField by tag number? 我有一个问题:如果我想通过标签号获取NSTextField的值,该怎么办?

I create NSTextField dynamically and set tag for it by this code : 我动态创建NSTextField并通过以下代码为其设置标签:

for(int i=0; i<number;i++)
{
    NSTextField *ssid = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y,150,25)];
    [ssid setStringValue:[NSString stringWithFormat:@"SSID %d :",i +1]];
    [ssid setSelectable:NO];
    ssid.tag = i;
    [ssid setEditable:NO];
    [ssid setBordered:NO];
    [ssid setDrawsBackground:NO];
    [ssid setAutoresizingMask:NSViewWidthSizable];
    [contentView addSubview:ssid];
    label_Y -=30;
    [ssid release];

   NSTextField *ssid2 = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y,150,25)];
    [ssid2 setStringValue:[NSString stringWithFormat:@"SSID %d :",i +1]];
    [ssid2 setSelectable:NO];
    ssid2.tag = i;
    [ssid2 setEditable:NO];
    [ssid2 setBordered:NO];
    [ssid2 setDrawsBackground:NO];
    [ssid2 setAutoresizingMask:NSViewWidthSizable];
    [contentView addSubview:ssid2];
    label_Y -=30;
    [ssid2 release];

} }

And then i want get value of each NSTextField But i don't know how to get value of NStextfield by tag number? 然后我想获取每个NSTextField的值,但是我不知道如何通过标签号获取NStextfield的值? Thanks 谢谢

Let's say the tag is 7. 假设标记为7。

[contentView viewWithTag:7]

That returns the tag, if a subview (including the view itself, contentView in this case) has this tag or nil if none is found. 如果子视图(包括视图本身,在这种情况下为contentView )具有此标签,则返回该标签;如果未找到,则返回nil。 You are in charge with making sure to have the tags unique. 您负责确保标签具有唯一性。 For that you should never ever use 0 as tag value because 0 is the default value [contentView viewWithTag:0] would return contentView unless you set its tag to something else. 为此,永远不要使用0作为标记值,​​因为默认值为[contentView viewWithTag:0]会返回contentView除非您将其标记设置为其他值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM