How to get control class type from object using controlTextDidChange

 

Example code how to catch the active control id class while entering text using controlTextDidChange and isKindOfClass
Using:
Xcode 4.2.1
Mac OS X 10.7.2
Cocoa
 
Create from the objects (NSTextField or  NSComboBox) an Outlet delegate to your delegate controller in the gui so that controlTextDidChange  NSNotifications are send from these objects.

Place the code in your .m file 

 

-(void)controlTextDidChange:(id)sender

{

    if ([sender isKindOfClass:[NSNotification class]]) 

    {   

        NSNotification *test = sender;

        id object = [test object];

        

        if ([object isKindOfClass:[NSComboBox class]]) 

        {       

            NSComboBox *sender_object = object;

            [sender_object setStringValue:[object className]];

        }

        

        if ([object isKindOfClass:[NSTextField class]]) 

        {

            NSTextField *sender_object = object;

            [sender_object setStringValue:[object className]];

        }        

    }

}   

 
The code will catch text changes and if the object is of type  NSComboBox  or   NSTextField  a reference is created and the text is replaced per object by the classname.