Search code examples
objective-ccocoalabelnstextfield

How to distinguish between a label and a textfield in cocoa application?


I want to have a collection of all of my textfields in my view:

for ( NSView* view in [self.view subviews])
{
    if ([view isKindOfClass:[NSTextField class]]){
            [self.allTxtFields addObject:view];
    }
}

problem is, that Labels are happened to be NSTextField too!

how can I differ a textfield from label?


Solution

  • Eventually I went by the tag number: I gave all the TextFields numbers between 100 to 200, and check by that -

    if ([view isMemberOfClass:[NSTextField class]] && [view tag] >=100 && [view tag] <200){
            if ([view isAccessibilityEnabled]){
                NSTextField* tfView = (NSTextField*)view;
                [self.allTxtFields addObject:tfView];
            }
        }
    

    not the most elegant but it works with no fear from attributes changes...