Reputation: 1694
I am making an iPhone app in which I am using UIDatePicker
and displaying the selected the time in a actionsheet. In actionsheet the time is displaying with 10 mints interval.
textfield
value is set correctly when I do scroll the Datepicker
If I don't scroll the action sheet then direct click on select button then text field set the current time (11:56
) but I select in action sheet (11:50
) time. (textfield
) shows the incorrect value.
How can I set the textfield
's value (11:50
). Give me some solution.
Upvotes: 6
Views: 4136
Reputation: 3898
I dont know this way correct wrong but its working for me
-(void)selectedTime{
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
// [outputFormatter setDateFormat:@"h:mm a"];
[outputFormatter setDateFormat:@"HH:mm:ss"];
selectedTime = [outputFormatter stringFromDate:datePicker.date];
NSArray *temp=[selectedTime componentsSeparatedByString:@":"];
NSLog(@"%@",temp);
NSInteger mins=[[temp objectAtIndex:1]integerValue];
//check your interval here
if (mins<=29) {
selectedTime=[NSString stringWithFormat:@"%@:%@:%@",[temp objectAtIndex:0],@"00",@"00"];
}else if (mins>=31 && mins<=59){
selectedTime=[NSString stringWithFormat:@"%@:%@:%@",[temp objectAtIndex:0],@"30",@"00"];
}else{
// selectedTime = [outputFormatter stringFromDate:datePicker.date];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(SelectedValue:)]) {
[self.delegate SelectedValue:selectedTime];
}
[self.popupController dismiss];
}
My interval is 30 mins
Upvotes: 0
Reputation: 38249
EDIT Make your class to handle UIControlEventValueChanged event in your picker. Add this line ViewDidLoad method or when your actionsheet shown:
[picker addTarget:self action:@selector(dateChanged:)
forControlEvents:UIControlEventValueChanged];
Now selector would be called when date is changed
UPDATE
- (void) dateChanged:(id)sender{
// handle date changes and set in textField
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *strdate = [dateFormatter stringFromDate:self.datePicker.date]; //provide your selected date here
self.dateTexField.text = strdate;
}
If above method is not called means date is not changed so always set default date in your textField.
Convert date to string object
UPDATE
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *strdate = [dateFormatter stringFromDate:self.datePicker.date]; //provide your selected date here
Now set value to textField like this:
self.dateTextField.text = strdate;
Upvotes: 1
Reputation: 2226
I think the easiest way is just to attach a toolbar with a done button to your TextField that has the UIDatePicker as its inputView. Keep track of your current TextField by maintaining a reference to it in TextViewDidBeginEditing. Then in the done button do something like this:
if (_currentTextField == _myTextFieldThatUsesADatePicker) {
self.myDateProperty = _myDatePicker.date;
}
Upvotes: 0
Reputation: 31091
Just set the dateSelected from your date picker after select button press....
NSDate *date = datePicker.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [formatter stringFromDate:date];
yourTxtField.text = dateString;
Upvotes: 1