I'm trying to make a UIDatePicker which allows the selection of hours only between 6.00 and 17.00. The other hours shall be inactive but still visible.
I think it can be done with setting up the picker calendar and then use this:
- (NSRange)maximumRangeOfUnit:(NSCalendarUnit)unit
but I'm not sure how.
Maybe someone can help me.
Thank you!
Sorry, but UIDatePicker doesn't support setting valid time ranges like you describe. The only options are minimum and maximum dates.
You'll either need to build a custom control from scratch or try to subclass UIDatePicker, but that's not a path I'd recommend.
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 want to to use custom datePicker in my project. In Mode date it shows Month,Day and Year, in my case I don't need Year, I just want to display Month and day.
The API does not provide a way to do this. If you want to be able to, please file a report requesting this ability, and it'll hopefully get included in a future release of iOS.
In the meantime, you can make a pretty convincing replica yourself using a UIPickerView.
Im sorry but following the reference of the PickerView
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIDatePicker_Class/Reference/UIDatePicker.html#//apple_ref/doc/c_ref/UIDatePickerMode
It says that is not possible to do that, so your only option is to implemente yourself the delegate and the datasource, and create the pickerview with the data you need.
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.
I want to show only day and month and not year in my date picker. Not sure how to achieve this. Please suggest.
you'll find the answer by Googling this and clicking on the first entry which happens to be from Stackoverflow also: UIDatePicker show only month and day
Hope it helps!
I solved this by setting the maximumDate property to current date. This disabled the dates from current date.