UIDatePicker.date bug - objective-c

I'm having a problem getting the date from UIDatePicker. I know the code of getting the date, but it just keeps on getting the CURRENT date, not the date form the picker.
- (IBAction)buttonPressed:(UIButton *)sender {
NSDateFormatter *formate = [[NSDateFormatter alloc]init];
NSDate *settedDate = self.myPickedDate.date;
[formate setDateFormat: #"dd.MMM.yyyy # HH:mm:ss"];
NSString *dateString = [formate stringFromDate:settedDate];
NSLog(#"datestring: %#", dateString);
}
I set myPicker to 15th May 2015 15:30 and Xcode logs out the current date (if it's 16:19 he will log out 22nd.Apr.2015 16:19, no matter what.
Xcode 5.1.1 on simulator iOS 7.1.2 (haven't tried on real device).

The problem is that in your viewDidLoad you are saying this:
self.myPickedDate = [[UIDatePicker alloc] init];
So, think about that code. What you are doing there is creating a new date picker, completely different from the one in your interface, and substituting it for the one in your interface, to which self.myPickedDate was previously set (because it is an outlet). So from now on, self.myPickedDate refers to a different date picker, one that is not in your interface (it is merely held in memory)! Therefore, nothing you do in the interface, such as setting the date in the date picker you see there, has any effect on self.myPickedDate.
Therefore, to solve the problem, delete that line of code.

Related

NSDateFormatter dateFromString returns nil for parsing EXIF data

I am trying to parse data from EXIF date to NSDate. Here is a sample date string taken from an image file:
2013:10:15 19:19:31
It is in year:month:day 24hour:minute:second format. I am using the following code to parse this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:#"yyyy:MM:dd HH:mm:ss" allowNaturalLanguage:YES];
formatter.formatterBehavior = NSDateFormatterBehavior10_4;
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
NSDate *date = [formatter dateFromString:dt];
If I don't set the formatter behavior it complains about trying a locale identifier of 10.4 on an 10.0 formatter. (I'm on Mavericks, I think it's a new issue on Mavericks) Anyway I've tried combinations of not setting locale at all, setting locale to en_US_POSIX instead of just en_US, setting a timezone for formatter, changing yyyy to YYYY in date format, and allowNaturalLanguage to NO, however, dateFromString always returns nil. My app is running on OS X Mavericks, my system's language is English, and my region is set to Turkey if it matters. I want my app to be English-only, independent of users' region (including the date formats). I've seen this question: Parsing EXIF date string to NSDate but even I try the exact steps, formatter returns nil for dateFromString. What is wrong with the date formatter?
It seems to be a problem of the initWithDateFormat initializer.
According to the documentation, it creates a formatter that uses the "OS X v10.0 formatting behavior", but I have no idea what that is. And setting the behavior to NSDateFormatterBehavior10_4 explicitly seems also not to help.
Using
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy:MM:dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:#"2013:10:15 19:19:31"];
produces the expected result.

Comparing two NSDates constantly even outside the app

I decided for my first application i would create an alarm application. So far i understand to create two NSDate objects and compare them with isEqualToDate and UILocalNotifications for notifications. How do i continually compare the set date to the current date. Do i create a loop, or is there a more efficient way on Objective C? How do i continually check in the background?
[very new to objective c, sorry and thank you]
This what iv'e started:
NSDate * now = [NSDate date];
NSDate * later = [[NSDate alloc] initWithString:#"2001-03-24 10:45:32 +0600"];
NSComparisonResult result = [later compare:mile];
NSLog(#"%#", later);
NSLog(#"%#", mile);
You don't need to create loops or compare dates yourself. One way to have your alarm sound could be via a local notification. A quick sample code to schedule yourself a local notification:
// Initialize your notification
NSUserNotification *notification = [[NSUserNotification alloc] init];
// Set the title of your notification
[notification setTitle:#"Alarm finished!"];
// Set the text of your notification
[notification setInformativeText:#"My Text"];
// Set the time and date on which the notification will be delivered (in this case 60 seconds after the current time)
[notification setDeliveryDate:[NSDate dateWithTimeInterval:60 sinceDate:[NSDate date]]];
// Set the sound, this can be either nil for no sound, NSUserNotificationDefaultSoundName for the default sound)
[notification setSoundName:NSUserNotificationDefaultSoundName];
// Schedule the notification
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];
If you don't want to use a local notification but want to execute something different once your alarm finishes, you could use NSTimer to execute your action once the alarm fires.

Custom UIPickerView with locale compatibility

I am making a custom TimePicker from a UIPickerView. I have it working great except when it comes to displaying the hours. I want it to function just like the UIDatePicker so that it follows the users Locale setting. If they have French selected, the time should display in 24 hour format. If they have US it should display in 12 hour format.
I have an array hoursArray that goes from 0 - 23. I then run this code to convert it to the needed format. However it is not wanting to change to 24 hour format if that option is set in the Settings -> International section
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];//[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"H"];
NSDate *tempDate = [dateFormatter dateFromString:[hoursArray objectAtIndex:row]];
[dateFormatter setDateFormat:#"h"];
return [dateFormatter stringFromDate:tempDate];
I tried changing [dateFormatter setDateFormat:#"h"]; to [dateFormatter setDateFormat:#"H"]; since Apples doc says the users personal locale setting will override it, but this doesn't seem to be the case.
How do I get the dateFormatter to follow the users locale setting properly?
In my case, the overriding of users preferences worked fine, and that was a bad thing for me.!!! Try setting the locale of your date formatter as
[dateFormatter setLocale:[NSLocale systemLocale]];
If that didnt work, try removing the code where you set the locale because the NSDateFormatter automatically considers the user settings.

UIDatePicker for time returns very strange results

I've searched the forum and there are quite a few posts on strange behavior of the UIDatePicker, but none of the solutions solved my problem.
I have a simple UIDatePicker initialized like this:
self.picker = [[UIDatePicker alloc] init];
self.picker.datePickerMode = UIDatePickerModeTime;
self.picker.calendar = [NSCalendar currentCalendar];
self.picker.timeZone = [NSTimeZone localTimeZone];
self.picker.date = self.initialDate;
[self.view addSubview:self.picker];
This is done in the viewDidLoad method of a simple view (no xib, no interface builder, just plain code).
Now I have a button, which retrieves the date and "processes" it.
NSDate* selected = self.picker.date;
NSLog(#"%#", selected);
NSDateComponents* components = [self.picker.calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:selected];
NSLog(#"0001-01-01 %02i:%02i:%02i +0000", components.hour, components.minute, components.second);
[components setSecond:0];
NSLog(#"0001-01-01 %02i:%02i:%02i +0000", components.hour, components.minute, components.second);
selected = [self.picker.calendar dateFromComponents:components];
NSLog(#"%#", selected);
Since the date picker will not allow the user to set seconds, I just want to reset them to zero (because I need to compare dates later).
When I select "16:00" (which is 4:00 pm) The log output is:
2012-05-21 17:43:48.428 Green Thumb[26828:fb03] 0001-01-01 14:55:36 +0000
2012-05-21 17:43:48.428 Green Thumb[26828:fb03] 0001-01-01 16:00:56 +0000
2012-05-21 17:43:48.429 Green Thumb[26828:fb03] 0001-01-01 16:00:00 +0000
2012-05-21 17:43:48.429 Green Thumb[26828:fb03] 0001-01-01 14:54:40 +0000
So the original date taken from the picker is completely off. The time taken from the components (which is created based on the date from the picker) is correct, as is the corrected time. But when I put the components back in a NSDate instance, it all gets messed up again (but differently).
Me and google we are both out of ideas, so I hope we can create some new ones here!
Thanks in advance, LetzFlow
PS: I'm testing this with the iPhone 5.1 Simulator.
EDIT #1:
I've tracked down the actual problem, but I'm stuck again:
I've tracked it down to the following point: I'm storing the dates in the NSUserDefaults object.
NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:[NSNumber numberWithFloat:[time timeIntervalSince1970]] forKey:#"notification.time"];
And here I get them out again:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber num = [defaults valueForKey:#"notification.time"];
NSDate* time = [NSDate dateWithTimeIntervalSince1970:[num floatValue]];
Interesting enough: What goes in, is not what comes out! But why? :)
EDIT #2:
And last but not least I've solved it myself. Thankfully, because it's the most stupidest mistake ever.
NSTimeInterval is not a float but a double. And while up until now I never ran into any problems with using a float saving it in the NSUserDefaults seems to have brought forward the errors.
Your NSLog statements seem to have the wrong format (i.e. the "0001-01-01" part). Try this instead:
NSString *output = [NSDateFormatter localizedStringFromDate:selected
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterShortStyle];
NSLog(#"%#", output);

How to use the same UIDatePicker to get Date and Time in 2 UITextField by separate

I'm working in iPad Application and I have a Picker in One XIB, I wanna use the same UIDataPicker to get by separate the date in one UITextField and The Time in a different UITextField.
Example:
UItextField_1 > Here I wanna get only the date
UItextField_2 > Here I wanna get only the time
UIDatePicker > I wanna this picker (only ONE) to get both values.
Some ideas to achieve it?
Any idea is welcome, Thank you very much!
You could set up an event listener using UIControlEventValueChanged in your viewDidLoad method.
[datePicker addTarget:self action:#selector(action:forEvent:) forControlEvents:UIControlEventValueChanged];
Then in that handler you could do something like this:
- (void)action:(id)sender forEvent:(UIEvent *)event {
// Date
dateTextField1.text = [NSDateFormatter localizedStringFromDate:[datePicker date] dateStyle:kCFDateFormatterShortStyle timeStyle:kCFDateFormatterNoStyle];
// Time
dateTextField2.text = [NSDateFormatter localizedStringFromDate:[datePicker date] dateStyle:kCFDateFormatterNoStyle timeStyle:kCFDateFormatterShortStyle];
}
[Note: this is untested code. It's only meant to give you an idea of what to do]
you just have to get the date from the UIDataPicker and use NSDateFormatter to separate the values ex
-(IBAction) endDate{
//gets date only 01/01/2011
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterShortStyle;
NSLog(#"%#",[df stringFromDate:endByDatePicker.date]);
//gets time h:mm
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"h:mm"];
NSLog(#"%#",[outputFormatter stringFromDate:self.endByDatePicker.date]);
}