0

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?

Lost Sorcerer
  • 905
  • 2
  • 13
  • 26

1 Answers1

1

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

Sierra Alpha
  • 3,707
  • 4
  • 23
  • 36
  • 1
    This is a good answer. We use a similar technique quite a bit. In addition to unique tags on text fields, we add a private `UITextField` property to the view controller in question to track the current-editing text field (with the property being assigned in -textFieldDidBeginEditing:). – Joshua Smith Jul 23 '12 at 19:08
  • I did not go as far as tagging the text fields, but this worked perfectly. I just grabbed the current text fields undo manager into a ivar, and when done editing set the ivar to nil. – Lost Sorcerer Jul 23 '12 at 19:39