Text from UITextFieldTextDidChangeNotification

12,177

The notification's object property stores the text field whose text changed, so notif.object.text would contain the text "r".

Share:
12,177
daihovey
Author by

daihovey

zora, ios

Updated on July 18, 2022

Comments

  • daihovey
    daihovey almost 2 years

    I have a UITextField with this NSNotification:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:@"UITextFieldTextDidChangeNotification" object:_searchTextField];
    
    - (void)textFieldDidChange :(NSNotification *)notif 
        {
         //
        }
    

    The NSLog is when I type in r

    NSConcreteNotification 0x193c20 {name = UITextFieldTextDidChangeNotification; object = <UITextField: 0x15a980; frame = (20 20; 280 31); text = 'r'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x15aad0>>}
    

    How do I get the text r out of the notif object?

  • daihovey
    daihovey about 12 years
    Its giving an error - Property 'text' not found on object of type 'id'
  • yuji
    yuji about 12 years
    Do [(UITextField*)notif.object text] instead
  • daihovey
    daihovey about 12 years
    Ah yep, so I had to: UITextField* txt = (UITextField*)notif.object; NSlog(@"%@",txt.text);