PDA

View Full Version : put UIDatePicker value into TextField?




Brendan.Porter
Jul 31, 2008, 10:03 PM
I'm trying to simply place the TIME from a UIDatePicker into a UITextField.
I am beating my head against the wall looking at the code for the last week.

The code I have is currently:
-(void) createDatePicker
{
[myDatePicker = [UIDatePicker alloc] awakeFromNib];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString* currentTime = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];

}

In my head the 7th line's "currentTime" should hold the date now in a string. However, in another method when I try to set the text of the UITextField to currentTime, nothing happens.

- (IBAction) buttonPress: calculateButton
{
[timeInField setText:currentTime];
}

Any help?



springframework
Aug 1, 2008, 04:35 PM
currentTime is delcared inside the function 'createDatePicker' so its scope is limited to that function.

Brendan.Porter
Aug 1, 2008, 06:12 PM
Ok, I get that.
I have a declaration of currentTime as an NSString in the header, so that isn't necessary.

I've changed the code a bit, and now do not receive warnings.
However, the text from theTime still does not put itself into the UITextField.

I've even prototyped the plumbing by placing a test string within the function that is supposed to put the date into UITextField, and that string does show in the field properly when the button is pressed. This tells me that I'm missing a critical piece of plumbing to either get the value of [myDatePicker date] into theTime, or theTime looses the value outside of the createDatePicker function.
I even tried a [theTime retain], but that didn't help.

-(void) createDatePicker
{
[myDatePicker = [UIDatePicker alloc] awakeFromNib];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
theTime = [dateFormatter stringFromDate:[myDatePicker date]];
[dateFormatter release];

}


- (IBAction) buttonPress: calculateButton
{
NSString* tryThisString = @"Test";
[timeInField setText:[tryThisString description]];
}



Any further help would be greatly appreciated.

springframework
Aug 5, 2008, 12:12 PM
it may be that you didn't link it to that function properly inside the Interface Builder.