Question

I have an app with a custom number pad. I need the custom number pad for some, but not all of the text fields on a preferences view. I have written this method to set up the number pad for certain text fields:

-(void)setNumberPadFor:(UITextField*)textField andNibName:(NSString *)nibNamed{

    textField.inputView = [[[NSBundle mainBundle] loadNibNamed:nibNamed
                                                         owner:self
                                                       options:nil] lastObject];

    UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                style:UIBarButtonItemStyleDone
                                                               target:textField
                                                               action:@selector(resignFirstResponder)];

    UIToolbar *tips = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    tips.barStyle = UIBarStyleBlackOpaque;
    [tips setBackgroundColor:[UIColor blueColor]];
    [tips setTintColor:[UIColor whiteColor]];
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                           target:nil
                                                                           action:nil];
    [tips sizeToFit];
    tips.items = [NSArray arrayWithObjects:spacer, btnDone, nil];
    textField.inputAccessoryView = tips;
}

I am calling the setup method as follows (in viewDidLoad for the ViewController):

[self setNumberPadFor:txtMinFeet andNibName:@"PosNegNumberPad"];
[self setNumberPadFor:txtMaxFeet andNibName:@"PosNegNumberPad"];

So, in my button click events, how do I access the text field that initiated the number pad?

-(IBAction)btnNum1:(id)sender{
    [[UIDevice currentDevice] playInputClick];
    // update text field
}
-(IBAction)btnNum2:(id)sender{
    [[UIDevice currentDevice] playInputClick];
    // update text field
}

Eventually I will have two custom number pads to cover the requirements, but for now there is just one (hence one nib currently passed as a parameter).

My button actions are set up and working, that is, I can set a break point inside them and verify the app is responding to the events.

What I am unclear about is how to identify which text field initiated the number pad so I can update it accordingly.

Obviously I'm leaving something out. Any suggestions? Thanks!

Was it helpful?

Solution

One way is to use an ivar and every time you get the delegate method textFieldDidBeginEditing: you set the ivar. Later, you query the ivar to get the current text field.

The other way to do is is to find the first responder. This means traversing the subviews of your primary view to find it. This is common enough that you can find code to do that here for sure. The other way is the easiest to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top