Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Brendan.Porter

macrumors member
Original poster
I cannot get my app to return the value from the DatePicker, and it is incredibly frustrating.

Can anyone help with suggestions to create a DatePicker, initialize it, set it to spit out the current selection to an NSString?

I really need help.

Thanks.

Here's what I have so far:

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

}

And I'm currently receiving a warning "void value not ignored as it ought to be"... a warning I was not receiving before today (after changing too much code to remember).
 

Brendan.Porter

macrumors member
Original poster
Sorry to reply to my own thread... but here's my revised code.

-(void) awakeFromNib
{
myDatePicker = [[UIDatePicker alloc]init];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:(NSString*) @"h:mm a"];
[myDatePicker addTarget:self action:mad:selector(controlEventValueChanged:) forControlEvents:UIControlEventValueChanged];
theTime = [dateFormatter stringFromDate:[myDatePicker date]];

[theTime retain];
}

- (IBAction) buttonPress: calculateButton
{
timeInField.text = theTime;
}

The above code works to display *THE CURRENT TIME* and NOT the time displayed in the Picker. Progress....

Any help?
 

Niiro13

macrumors 68000
Feb 12, 2008
1,719
0
Illinois
The reason why your first code doesn't work is that you never should call awakeFromNib (now I can see you fixed it in your second code). Because awakeFromNib doesn't return anything, you're pretty much setting the datepicker object as nil and not an initialized datePicker.

The second code theTime is simply a string that gets the date from the picker then retains the string of that date. Therefore, you receive the same value again and again.

What you should do is move all the dateFormatter stuff into the buttonPress action:

PHP:
- (IBAction) buttonPress: calculateButton
{

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:(NSString*) @"h:mm a"];

timeInField.text = [dateFormatter stringFromDate:[myDatePicker date]];
}

If that doesn't work it has something to do with the dateFormat as that's the only one I'm unfamiliar with.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.