Search code examples
iosuitoolbar

Determine textfield from toolbar IOS


I am trying to set up undo and redo for each textfield and unsure how to figure out how to determine which text field is the first responder.

Is there an argument I can pass into the methods called by the buttons from the toolbar, or do I need to do some fancy footwork?


Solution

  • This is an idea:

    If the viewController becomes delegate of each textField, then the viewController will get notified as each textField's value changes, or becomes first responder.

    To adopt the delegation, you will do:

    @interface MyViewController : UIViewController <UITextFieldDelegate>
    @end
    
    @implementation
    - (void)someMethod{
        // for a series of textfields
        myTextfield1.delegate = self;
        myTextfield1.delegate = self;
       // or you hook the delegate in IB
    }
    
    // then you get notified
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        // textField here that gets passed in as an argument is the first responder
        // if you have, let's say tag number for each
        NSInteger activeTextFieldTag = textField.tag;
    }
    @end
    

    Here is the reference to UITextFieldDelegate Protocol