Question

I have a problem : How can i do If i want get value of NSTextField by tag number?

I create NSTextField dynamically and set tag for it by this code :

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? Thanks

Was it helpful?

Solution

Let's say the tag is 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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top