I am using UIDatePicker control in my project. Added the UIDatePicker in xib file, and programmatically setting the constraints such as minimumDate, maximumDate to the datePicker. This is working fine in simulator, but in device I am able to select any date in datePicker.
Sample code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.datePicker setMinimumDate:[NSDate date]];
[self.datePicker setMaximumDate:[[NSDate date] dateByAddingTimeInterval:3*24*60*60]];
}
screenshot:
Note: I am working on xcode5 - DP6.
Thanks in advance.
Add an action for the UIControlEventValueChanged event on the UIDatePicker and implement checks to see if the selected date is greater than maximum date or less then the minimum date and set the date accordingly, like so:
- (IBAction)dateValueChanged:(id)sender
{
NSTimeInterval date = [self.datePicker.date timeIntervalSince1970];
NSTimeInterval maxmimumDate = [self.datePicker.maximumDate timeIntervalSince1970];
NSTimeInterval minimumDate = [self.datePicker.minimumDate timeIntervalSince1970];
if (date < minimumDate) {
self.datePicker.date = self.datePicker.minimumDate;
}
if (date > maxmimumDate) {
self.datePicker.date = self.datePicker.maximumDate;
}
// Use the selected date
NSLog(#"%#", self.datePicker.date);
}
This will cause the UIDatePicker to scroll to the max/min date if you select a date outside the range.
Related
I am using FSCalendar https://github.com/WenchaoIOS/FSCalendar I have a button in my VC and FSCalendar in UIView If I click on the button I need to show Current date(some thing like google calendar app) how can I do this ?
You can call the calendar selectDate with today's date in the button action. like below:
NSDate *today = [NSDate date];
[calendar setDate: today];
When you tap the button this will scroll to today with animation and select it.
I hope this helps.
If you want to show the month of today.
calendar.currentPage = [NSDate date];
If you want to 'select' today and show the month of today
[calendar selectDate:[NSDate date]];
Button Action:
- (void)backToToday {
// scroll to page date
self.currentPageDate = self.today;
// default selected date
self.selectedDate = self.today;
self.calendar.currentPage = self.currentPageDate;
[self.calendar selectDate:self.selectedDate scrollToDate:NO];
}
This is a dynamic prototype UITableView: In my cellForRowAtIndexPath:
I have dequequed my custom cell and this works just fine:
TimeSetViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:#"timeCell" forIndexPath:indexPath];
Next I set TimePicker to UITextField in timeCell and this works fine as well (i.e. the time picker displayed onclick of UITextField in timeCell) :
// Start Time
UIDatePicker *sTimePicker = [[UIDatePicker alloc]init];
sTimePicker.datePickerMode = UIDatePickerModeTime;
sTimePicker.backgroundColor = [UIColor whiteColor]
[cell.startTripTime setInputView:sTimePicker];
// End Time
UIDatePicker *eTimePicker = [[UIDatePicker alloc]init];
eTimePicker.datePickerMode = UIDatePickerModeTime;
eTimePicker.backgroundColor = [UIColor whiteColor];
[cell.endTripTime setInputView:eTimePicker];
Next I wanted to update the cell's UITextField through an event, in which I have understood from #Mani's post, I have set:
sTimePicker.tag = 1;
eTimePicker.tag = 2;
[sTimePicker addTarget:self action:#selector(updateTextField:)
forControlEvents:UIControlEventValueChanged];
[eTimePicker addTarget:self action:#selector(updateTextField:)
forControlEvents:UIControlEventValueChanged];
And in my custom method -(void)updateTextField:(UIDatePicker *)sender I have included this:
//Date Formatter
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setTimeStyle: NSDateFormatterShortStyle];
if(sender.tag == 1){
//Get Start Date
UIDatePicker *starttimepicker = (UIDatePicker*)sender.inputView;
thisStartTime = [starttimepicker date];
//Display Date
NSString *startTimeText = [dateFormat stringFromDate:thisStartTime];
sTimeText = [NSString stringWithFormat:#"%#",startTimeText];
}
if(sender.tag == 2){
//Get End Date
UIDatePicker *endtimepicker = (UIDatePicker*)sender.inputView;
thisEndTime = [endtimepicker date];
//Display Date
NSString *endTimeText = [dateFormat stringFromDate:thisEndTime];
eTimeText = [NSString stringWithFormat:#"%#",endTimeText];
}
Question Update
I managed to pass startTimeText and endTimeText to cell.startTripTime.text and cell.endTripTime.text through sTimeText and eTimeText.
And I used [_tableView reloadData]; in -(void)updateTextField:(UIDatePicker *)sender (after the two if blocks) and the data is displayed in my cell.startTripTimeand cell.endTripTime UITextField, but it appears the same in across of my sections, while I want it to change only the first section's time, how can I fix that?
//I couldn't include the image but the output of the UITableView looks like this:
_________________________
Section 1
_________________________
Start Time : 4:50 PM
_________________________
End Time: 5:50 PM
_________________________
Section 2
_________________________
Start Time : 4:50 PM
_________________________
End Time: 5:50 PM
_________________________
And the [_tableView reloadData]; seems to interfere and reloads the view while I haven't even finish picking the time that I wanted, how can I fix this too?
The action method should be sent to one of the picker, the startStripTime seems to be a UIView instance not a UIDatePicker instance.
It is he date picker that changes its value, not the view containing it.
You should change also the update method accordingly.
Hey I am working on a small app that changes the text of the UIButton based upon the current state of the date selector. It either will show "What day was it?" or "What day will it be?". However with xcode 6.3 beta, the UIButton SetTitle method does not seem to exist. Instead I try to assign a string to titleLabel.txt, but this does not change the button text.
Method is as follows:
(IBAction) changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now = [NSDate date];
if(currentDate<now){
self.whatButton.titleLabel.text = #"What day was it?";
}
else {
self.whatButton.titleLabel.text = #"What day will it be?";
}
I have seen that the setTitle method would be a possible solution but discovered that the libraries may have been changed. Any suggestions would be greatly appreciated!
You should be setting your titles like this:
[self.whatButton setTitle:#"What day was it?" forState:UIControlStateNormal];
Use the following code to set the title of the button in different states as if for normal
[self.yourButton setTitle:#"Default Title" forState:UIControlStateNormal];
as if you have clicked your button for selected state it must be
[self.yourButton setTitle:#"Selected Title" forState:UIControlStateSelected];
as if you have clicked your button again for deselected state it must be
[self.yourButton setTitle:#"Selected Title" forState:UIControlStateDisable];
and many other states you can set different Text in every state of the button
Try changing the way you make the date comparison -
-(IBAction)changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now = [NSDate date];
NSString *titleString = ([currentDate timeIntervalSinceReferenceDate] < [now timeIntervalSinceReferenceDate]) ? #"What day was it?" : #"What day will it be?";
[self.whatButton setTitle:titleString forState:UIControlStateNormal];
}
thank you for all your suggestions. The first suggestion was a little off base since it was not the sender's title that I wanted to change. The second suggestion is a good idea that I will use in the future. I ended up solving the riddle myself with this:
- (IBAction) changeButton:(id)sender {
NSDate *currentDate = [self.datePicker date];
NSDate *now = [NSDate date];
UIButton *tempBtn = (UIButton *)self.whatButton;
UIControlState state = tempBtn.state;
if([currentDate compare:now] == NSOrderedAscending){
[tempBtn setTitle:#"What day was it?" forState:state];
}
else {
[tempBtn setTitle:#"What day will it be?" forState:state];
}
}`
I'm interested in registering, if the user hit the min or max date while using a UIDatePicker.
Is there a way to do so?
Thanks.
First, add a method that listens to value changes in the date picker itself:
[datePicker addTarget:self action:#selector(datePickerDateChanged:) forControlEvents:UIControlEventValueChanged];
Then, in the method, compare the date of the date picker with the min or max date:
- (void)datePickerDateChanged:(UIDatePicker*)picker {
if ([picker.date compare:picker.maximumDate] == NSOrderedSame) {
// max
} else if ([picker.date compare:picker.minimumDate] == NSOrderedSame) {
// min
}
}
Hope that hepls. Good luck!
I have a view with an UITextField and need to show an UIDatePicker (it's hidden below the keyboard) when a button is pressed.
I'm lazily instantiating the date picker because this allows me to show the view way more fast.
This is the code associated with the tap on the button:
- (IBAction)hideKeyboard {
if (!self.datePicker) {
// UIDatePicker allocation and initialization
self.datePicker = ...
...
[self.view addSubview:self.datePicker];
}
[self.textField resignFirstResponder];
}
What happens is that this interfere with the keyboard hiding animation. Actually there is no animation at all.
Another clue I have is that this doesn't happen on the simulator, but only on the actual device (an iPhone 4S).
How can I solve this?
EDIT:
Setting the datePicker as the inputView of the textField doesn't solve my problem.
I can recommend you another solution then hiding keyboard and using button for showing the date picker.
If I understood you correctly you want to show a date picker when user taps on the textfield and then maybe you set the date in textfield.
UITextField has a property
#property (readwrite, retain) UIView *inputView
If you set the inputView property to date picker then the textfield will show date picker instead a keyboard (and even animated like keyboard).
Of course the values need to get your own and set to textfield.
Edit reason: Question edited
I do not know why setting the datepicker as the input view did not help. Anyway if you do not want the keyboard shown and show the datepicker by adding a subview you can use the following delegate method of UITextField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
implement the delegate method. In the method just add pickerview as subview to your view and return NO for not to show the keyboard.
Edit reason: Commend
Is there any reason why you do not use textfield? If not I would use it, because it makes everything easier. If you want to use for some reason the UILabel then you can watch the keyboard notification. When did the keyboard disapeared and then show the datepicker.
For keyboard notifications please refer here, good explained with sample codes, UIKeyboardDidHideNotification should help you
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
Quite simple :
Just add a date picker object as the input value of your textfield. No need to show/hide the keyboard.
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init] ;
[comps setYear:anyInteger];
NSDate *maximumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:anyInteger;
NSDate *minimumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maximumDate];
[datePicker setMinimumDate:minimumDate];
datePicker.datePickerMode = UIDatePickerModeDate;
textField.inputView = datePicker;