I am using UIDatePicker in my iPhone application.I have kept a button on clicking which the mode of datepicker switches between UIDatePickerModeTime and UIDatePickerModeDate.
When I change the date of datepicker when the mode is UIDatePickerModeDate,the changed date correctly appears when I switch the mode but if I change the time and switch the mode,on again switching the mode to UIDatePickerModeTime resets the time to 12:00 AM.I am not getting why this is happening and what to do for it.Please help.
I have a similar problem. The first time I change from UIDatePickerModeDate to UIDatePickerModeTime, the time was OK. However, when I change back to the ..Date mode and then to the ..Time mode again, the time read 00:00. Thereafter, regardless of the number of mode changes, the time always remained at 00:00. I modeled my UIDatePicker programmatically the same as the UIDatePicker in Apple's UICatalog app. This behavior does not exist in their app.
Taking your lead, I saved the NSDate value from the picker in a retained #property with the intentions of retrieving and resetting the picker upon a UIControl Notification. However, I could not find an event that fired when the picker was changed from Date mode to Time mode. I even tried something like:
[myDatePicker addTarget:self action:#selector(getDateTime:) forControlEvents:UIControlEventAllEvents];
Of course the event fired if I changed the picker's date (or time) but as I mentioned, not when I changed the mode.
I eventually ended up using a UISegmentedControl (same as in the UICatalog app) that targeted the same event method above where I toggled the mode. However, when I reset the picker to the saved NSDate value, the Time remained at 00:00. I then used NSLog to verify that the saved value was OK and discovered that its Time value portion had also changed to 00:00! This was indeed strange since I had not saved a new NSDate value. The original saved value simply changed on it's own!
I'm still investigating the problem and am very close to calling it a bug, especially when a UIDatePicker is added programmatically without a supporting NIB.
Update:
My final solution was almost identical to yours. For some reason, the error continued when the saved date was stored as an NSDate value. Storing it as an NSString and typecasting both ways did the trick:
self.saveDate = (NSString*)self.myDatePicker.date;
...
[self.myDatePicker setDate:(NSDate*)self.saveDate animated:NO];
Also, since I'm retrieving and saving the date to an external device via a GCD dispatch_async block, the property, self. notation was also needed (even on the main_que).
Final conclusion:
I'm now convinced that setting the UIDatePicker date each and every time the picker is accessed is a necessary requirement rather than a bug. Using Apple's UICatalog app, when you set the date to today just once and then switch between Date and Time mode, their app exhibits exactly the same behavior. The date is retained but the time resets to 00:00. if you switch to a mode other than date or time and switch back, all modes will be reset to their floor values.
I used a Date object which I declared as a property in which I stored the date from the date picker when I present the date picker.
I updated the stored date when I changes the date in date picker(Using UIControl Notification)
On changing the mode of the date picker I assigned the same date to the date picker.
I understand this was fairly simple but formerly I was trying the same thing by taking a simple Date object which didn't work.Making it a property did the job for me
Related
I have a button which disables after it is pressed. I need To work out how to get the button re-enable after midnight, or the next day.
So I think I need the button to set the date in a variable after it is pressed. Then an if statement which enables the button if the variable does not match today's date.
Would that be the best way to handle it? Can anyone offer any rough code advice, or possibly advice of an easier way to handle enabling a button after midnight?
Thanks
Then an if statement which enables the button if the variable does not match today's date.
But the question is, an if statement where? Your code can only run if something makes it run. If your app is just sitting there, midnight can come and go and your button won't be enabled because the code that looks to see if it's a different day from the day you saved is not running.
The solution:
Register to be notified by the system when the day changes.
Woow finally some nice question... it's definitely possible... you can check this beautiful documentation by apple:
https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
Basically you have 2 or more solutions, just 2 comes to my mind right now:
1.) Use UserDefaults or CoreData (Userdefaults for one property is more acceptable I guess
2.) Run backgroundTask as mentioned above..
The first one is very basic solution... you get the date when the button was tapped and if the day is equal to the day you entered the screen later, you keep the button disabled, else you unlock the button and store again the date into the user defaults, this seems to me a little piggy if you ask me..
The second one is much more elegant, in a nutshell... You setup button with some actions and the last thing is disabling it. Then, you setup background Task that every next day the button becomes enabled again...
I am pretty sure you will find the codes somewhere else here, maybe you will find the first algorithm more useful for you..
Anyway wish you best luck and happy coding! :)
I want to have a time range on a UIDatePicker, so I can display what times users can book a meeting. Say on May 25th, they can book between 12pm and 8 pm, if they scroll to May 26th they can book between 4pm and 8pm.
So I'm wondering how I can display this in a UIDatePicker? Users can't choose time outside of a preset I have for each day.
Any help is appreciated!
I found this one, hope this will help you out..
Can UIDatePicker's minimumDate and maximumDate include time?
Enjoy Coding :)
Sorry, but no. UIDatePicker can only set ranges for dates. If you want to restrict the range of the hours, you will have to subclass uidatepicker and create your own custom class.
Here's a link: UIDatePicker hours interval (NSCalendarUnit?)
Hope this helped!
I would suggest using UIPickerView to make your own date picker. Every time a date is selected, you can change what times are available inside pickerView:didSelectRow:inComponent:.
What I ended up doing was to add a target to the date picker, and check if the time of the date chosen matched the hours I preset and display an alert view if they didn't match.
I have a medical app that allows the user to calculate a patient's age based on date of birth entered with a date picker. For convenience, I set the default date for the date picker object in Interface Builder to 1/1/1950. However, I noticed that in iOS 5 the default date no longer sets when the date picker initially loads. The default date automatically becomes the upper date limit (if that parameter enabled in Interface Builder), or the current date if this upper limit is not enabled. The default date field in Interface Builder still has "1/1/1950" set. The same version of the app on an older device compiled with iOS 4 still works properly, with the date picker scrolling to 1/1/1950 on loading. I very much would appreciate any advice for fixing the problem. Thanks in advance.
In your ViewController's viewDidLoad, log the value of the picker. If it is not set correctly, then you can try setting the value programmatically in viewDidLoad.
in my Core Data model, I have an entity which has got a date attribute. For example, I'll set this date to 10/07/2011 4:00pm and I want to fire an action when current date will pass the saved date by one minute.
A local notification will be fired but I also want to fire another method to change another entity attribute's value. Is it possible to do something like this?
I've also thought to NSTimer but I've never used them... And a last question: will this action fire always even is app isn't in background or foreground?
Thank you so much!
Matteo
You can't fire an action while running in background mode other than through local notifications.
To check if the date condition has been met while running in foreground, NSTimer is the way to go.
Try to check Local notifications
When using a UIDatePicker in iOS SDK, there is always an entry "Today" at the current date.
That's useful in most cases, but where I need it, it's rather confusing.
Is there a way to:
a) disable the "today"-entry (use regular date instead), and have all entries look the same
or even better
b) disable the "today"-entry (use regular date instead), and color the next day in blue
Further more, the application is for private use only, it's not going to get distributed on the AppStore, which means I could use private APIs (I still would rather avoid them) and I don't need it to be backwards compatible. iOS 4 is fine.
I had a similar problem with the UIDatePicker not matching my requirements exactly (in my case I needed a datepicker without a year wheel). Having a look at the UIDatePicker reference, it doesn't look like you can disable the today entry, so you might be forced to do what I did.
I used a UIPickerView and re-implemented the date selection functionality I needed with that. There are a few things you will need to do to implement your custom date picker:
Implement a UIPickerViewDataSource to set up row titles, dimensions and row counts.
Implement a UIPickerViewDelegate to handle events from your custom picker.
Make sure you update your day wheel when the month wheel changes so you get appropriate days for each month. UIDatePicker does this pretty seamlessly. With limited time, I just reload the picker when the month changes so the day counts match up.