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!
Related
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 want to know how to identify the UISlider is tracking towards maximum to its minimum values?
For ex, My slider maximum and minimum values 1.0 and 0.0. How can I identify my slider is tracking from 0.0 to 1.0 and vice versa?
Thanks!
There is an event of UISlider named value changed. Use that event and calculate the difference. If difference is positive then its going towards max value otherwise its moving towards min value. Use below action method.
- (IBAction)sliderdidchanged:(UISlider *)sender {
if (sliderOldValue - sender.value > 0) {
NSLog(#"Value Decreasing");
}
else {
NSLog(#"Value Increasing");
}
sliderOldValue = sender.value;
}
Don't forget to make reference to this method from UISlider object in your xib or Storyboard. Also declare sliderOldValue as instance variable and in viewDidLoad assign its value to the slider's default value.
.h
To track Last Value
#property(nonatomic,assign) float lastValue;
.m
in ViewDidLoad listen for UIControlEventValueChanged
[yourSlider addTarget:self
action:#selector(mySliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
Define the method
- (IBAction)mySliderValueChanged:(UISlider *)sender {
float currentValue = sender.value;
if (currentValue>self.lastValue) {
NSLog(#"moving Right");
}
else{
NSLog(#"moving Left");
}
self.lastValue = sender.value;
}
You can listen for the ValueChanged control event
[slider_ addTarget:self action:#selector(sliderMove:) forControlEvents:UIControlEventValueChanged];
...
- (void) sliderMove:(UISlider*)slider
{
if (slider.value == slider.minimumValue) {
// etc...
}
else if (slider.value == slider.maximumValue) {
// so on and so forth
}
}
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.
I follow this link but I have two textfield
How I use for two textfield.My code is here Date1 and Date2 are textfields.Here when I select date from textfield1 and textfield2 , textfield1 always takes value from textfield2
You need to set tag on every text field and then identify in donButtonPressed method and set text according to tag.
Otherwise
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if([textField isEqual:Date1])
{
Date1.text=#"YourDate";
}
else
{
Date2.text=#"YourDate";
}
}
To do that i always set a current TextField to know the textField. Then I use the currentTextField to modify the text when the pickerDate is validing by the user.
I don't need to use the textFieldDidEndEditing...
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
_activeTextField = textField;
}
Here you set equals the both textfield... That's why you edit Date1 and Date2 when you validate your date picker.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
Date1 = textField;
Date2 = textField;
you have to use textFieldDelegate for this. have add the below code in viewcontroller.h file.
#interface ViewController : UIViewController<UITextFieldDelegate>
Once you did editing in textfield2, the below method.
- (void)textFieldDidEndEditing:(UITextField *)textField{
textField1.text=textField2.text;
}
use this
dateTextField1.text = [NSDateFormatter localizedStringFromDate:[datePicker date] dateStyle:kCFDateFormatterShortStyle timeStyle:kCFDateFormatterNoStyle];
// Time
dateTextField2.text = [NSDateFormatter localizedStringFromDate:[datePicker date] dateStyle:kCFDateFormatterNoStyle timeStyle:kCFDateFormatterShortStyle];
So, textField1 always takes value from textField2.