0

If I have a set up like so:

- (void)textFieldDidBeginEditing:(UITextField* )textField {

    if (textField.tag == 603) { 

        datePicker = [[UIDatePicker alloc]init];
        [datePicker setDatePickerMode:UIDatePickerModeDate];
        [datePicker setDate:[NSDate date]];
        [datePicker addTarget:self action:@selector(showDate:) forControlEvents:UIControlEventValueChanged];
        textField.inputView = datePicker;

    }

}

in the setDate method, can I reference which textField the UIPickerDate is the inputView of? That sounds confusing but like something like this:

- (void) showDate: (UIDatePicker*) myDatePicker {

    NSDate* selected = [myDatePicker date];
    NSString* date = [selected description];
    myTextField = myDatePicker.view //I know this is not a property

}

kind of in the same sense that a UITapGestureRecognizer knows what view is calling it...

Explanation for Woz: I could but it would make the app less dynamic. This is a big form for the installation of medical devices. To create the form I store all the element properties in a dictionary (label, type (text, checkbox, segmented control), form section, etc.) and then each dict in an array in the proper order. I programatically build everything while in the array loop. I guess I could subclass UITextField...but you got to admit it would be nice to know which textfield initiated the inputView

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
  • 1
    Couldn't you store a pointer to the selected text field as a variable in your view controller? – woz Mar 29 '13 at 17:54
  • I replied in an edit to my post. – PruitIgoe Mar 29 '13 at 18:27
  • Hmm, I see your problem. It's too bad `addTarget` won't let you pass other information. – woz Mar 29 '13 at 18:38
  • : D Dang, I was just googling that... – PruitIgoe Mar 29 '13 at 18:39
  • Yea, I found questions like this: http://stackoverflow.com/questions/5690283/passing-custom-data-in-uibutton-addtarget But I didn't see a smooth solution for your situation. – woz Mar 29 '13 at 18:40
  • 1
    @PruitIgoe Just a hack, you can put the tag of textField to dataPicker. And from the handler you find a reference to the textField using the viewWithTag:myDatePicker.tag. – Anupdas Mar 29 '13 at 18:43
  • @Anupdas - I was using tag to differentiate the textfield data types - 603 is a date field, 602 numeric non currency, 601 string...I think I might just subclass it, but I didn't know about viewWithTag so that makes your response awesome and upvoted! – PruitIgoe Mar 29 '13 at 18:50
  • @PruitIgoe I have put the above the above recommendation as an answer. – Anupdas Mar 29 '13 at 19:02

3 Answers3

0

I just subclassed UITextField and got it to work:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.textColor = [colorManager setColor:66.0 :66.0 :66.0];
        self.font = [UIFont fontWithName:@"Helvetica" size:18.0];
        self.backgroundColor = [UIColor whiteColor];
        self.borderStyle = UITextBorderStyleRoundedRect;
        self.returnKeyType = UIReturnKeyDone;
        self.userInteractionEnabled = YES;

        datePicker = [[UIDatePicker alloc]init];
        [datePicker setDatePickerMode:UIDatePickerModeDate];
        [datePicker setDate:[NSDate date]];
        [datePicker addTarget:self action:@selector(setDate:) forControlEvents:UIControlEventValueChanged];
        self.inputView = datePicker;
    }
    return self;
}

- (void) setDate : (UIDatePicker*) myDatePicker {

    NSDateFormatter* myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"MM/dd/yyyy"];

    NSString* dateString = [myDateFormatter stringFromDate:myDatePicker.date];
    self.text = dateString;
}
PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
0

The UITextField that triggered the UIDatePicker would be the current firstResponder. You can use this info to identify the textfield that is associated with the currently active UIDatePicker.

sixthcent
  • 1,150
  • 7
  • 6
-1

Add a tag to UIDatePicker and use the tag to find textField from view.

- (void)textFieldDidBeginEditing:(UITextField* )textField {

    if (textField.tag == 603) { 

        datePicker = [[UIDatePicker alloc]init];
        [datePicker setTag:textField.tag];
        [datePicker setDatePickerMode:UIDatePickerModeDate];
        [datePicker setDate:[NSDate date]];
        [datePicker addTarget:self action:@selector(showDate:) forControlEvents:UIControlEventValueChanged];
        textField.inputView = datePicker;

    }

}

- (void) showDate: (UIDatePicker*) myDatePicker {

    NSDate* selected = [myDatePicker date];
    NSString* date = [selected description];

    UITextField *textField = (UITextField *)[self.view viewWithTag:myDatePicker.tag];

}
Anupdas
  • 10,211
  • 2
  • 35
  • 60