I am working with the UIdatepicker. I am fetching the time from the datepicker and showing the date on the label. Once i select the date it is shown on the label and datepicker is hidden away. Now i want that when i call the picker it should show me the selection bar of the datepicker on the date of the label which i selected previously selected on the datepicker.Code i am using is shown below:-
-(void)datepickershown:(UILabel *)time animated:(BOOL)animated
{
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:#"Select the Time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"DONE" otherButtonTitles:Nil, nil];
pickdate = [[UIDatePicker alloc] initWithFrame:[actionsheet bounds]];
pickdate.hidden = NO;
//pickdate.date = [NSDate date];
pickdate.datePickerMode = UIDatePickerModeTime;
[pickdate addTarget:self action:#selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
[actionsheet addSubview:pickdate];
[actionsheet showInView:self.view];
[actionsheet setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickdate.bounds;
pickerRect.origin.y = -90;
pickdate.bounds = pickerRect;
}
- (IBAction)changeDateInLabel:(id)sender
{
NSLog(#"I Am Changing the values");
df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterShortStyle;
}
As you want the datepicker to show the date you have already set on the label.
For this you need to use following method.
- (void)setDate:(NSDate *)date animated:(BOOL)animated
Pass the date whatever you have selected and is shown in the label.
try this
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
if([dateFormat dateFromString:yourDateLabel.text])
{
NSDate *date = [dateFormat dateFromString:yourDateLabel.text];
[self.datePicker setDate:date];
}
hope this will work
Related
If I am selecting date from date picker view for current date, the time is also getting selected and displaying the current time, I want to separate in two different selection event
Here is my code snippet-
/* Button for displaying UIDatePicker view */
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM, yyyy"];
self.lbl_StartDateValue.text = [dateFormatter stringFromDate:[sender date]];
self.planStartDate = [dateFormatter stringFromDate:[sender date]];
NSDate *date = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:[dateFormatter dateFromString:[dateFormatter stringFromDate:[sender date]]] options:0];
self.lbl_EndDateValue.text = [NSString stringWithFormat:#"End %#",[dateFormatter stringFromDate:date]];
self.planEndDate = [dateFormatter stringFromDate:date];
}
// Here I am not able to differentiate the selection of two picker view
-(void)onDoneButtonClick {
[self dueDateChanged:self.picker];
[self dueTimeChanged:self.picker];
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
[self.tempBackgroundView removeFromSuperview];
}
-(void) dueTimeChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
self.lbl_timeValue.text = [dateFormatter stringFromDate:[sender date]];
self.planAlarmtime = [dateFormatter stringFromDate:[sender date]];
}
do like , initially set the current date on your text field whenever user present the date picker
(IBAction)btn_calendar_event:(id)sender {
......
[self.toolbar sizeToFit];
[self.view addSubview:self.toolbar];
//if you want to show the default date as current date
self.picker.date = [NSDate date];
// set inital date on your textfield
[self dueDateChanged : self.picker];
}
if user pressed the done button then call this
-(void)onDoneButtonClick {
// call the method
[self dueDateChanged : self.picker];
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
[_txtf_birthDate resignFirstResponder];
}
finally change the YYYY format
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSLog(#"Picked the date %#", [dateFormatter stringFromDate:[sender date]]);
_txtf_birthDate.text = [dateFormatter stringFromDate:[sender date]];
}
Just set minimum data as your current date
[self.picker setMinimumDate:[NSDate date]];
also you will not select until you scroll Picker
if you want to show the default date as current date
self.picker.date = [NSDate date]
this is the code for UIDatepicker alloca and set current date
UIDatePicker *datepicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-320, self.view.frame.size.width, 320)];
datepicker.date = [NSDate date];
[self.view addSubview:datepicker];
I have searched similar questions here to help me solve mine, all in vain. I want the UItextField when clicked to pull up the UIDatePicker, then the user can select a date, and then set it in the UITextfield. However I keep getting an error for 'datePicker'. not sure what else I am supposed to do.
This is the part of my app that calls the datePicker
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your spesific mode
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:#"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}
-(IBAction)dateChanged{
NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US"]];
[FormatDate setDateFormat:#"dd.MM.yyyy"];
pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
}
You're not going to be able to Google the cause of the errors in your code. You will need to figure it out, as your code is unique.
As #madmik3 points out, this line is wrong:
pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
That code is trying to call a UIDatePicker class method "datePicker", which doesn't make any sense.
What you probably want instead is:
pd.text = [FormatDate stringFromDate: [datePicker date]];
That code is asking your date picker object, in the variable datePicker, for it's date property (the current date set in the date picker.
Hi in my application i have a registration form in my application and I'm validating all fields like email and phone number. But i want to validate this DOB in format like DD/MM/YY. I'm using else if condition checking for my validation i want do same for DOB
I'm validation with else if condition like this.
- (IBAction)send:(id)sender {
if ([self phonevalidate:[ph text]]!= 1){
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"Message" message:#"pls enter valid 10 digit number" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert1 show];
[alert1 release];
}
else if ([self validateEmail:[emd text]]!= 1){
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"Message" message:#"pls enter valid email id" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert1 show];
[alert1 release];
}
My validation function for phone.
-(BOOL)phonevalidate:(NSString *)phh
{
NSString *phoneRegex = #"[0-9]{10}";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", phoneRegex];
return [phoneTest evaluateWithObject:phh];
}
I'm not using the UIDate Picker i have UITextfield for the DOB i want validate with UITextfield itself.Like the above i want to validate the DOB please tell me how to validate the DOB and check int else if condition.
Thanks.
EDITED:
Assume that b'date is
NSString *dateOfBirth = #"22/03/14";
And this is method for check b'date is valid or not ?
-(BOOL) isValidateDOB:(NSString *) dateOfBirth
{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateStyle:NSDateFormatterShortStyle];
[format setDateFormat:#"dd/MM/yy"];
NSDate *validateDOB = [format dateFromString:dateOfBirth];
if (validateDOB != nil)
return YES;
else
return NO;
}
In above method [format setDateStyle:NSDateFormatterShortStyle]; and setDateFormat is very important in your case:
Now you can check it as like,
if([self isValidateDOB:dateOfBirth])
{
// b"date is valid;
}
else
{
// b"date is not valid;
}
You can use UIDatePicker for this :
Initialize like this :
UIDatePicker datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.date = [NSDate date];
Then, set the inputView of your textField (the one that takes DOB as input) :
yourDOBtextField.inputView = datePicker;
Then, implement textFieldDidEndEditing: delegate method for UITextField:
- (void) textFieldDidEndEditing:(UITextField *)textField {
if (textField == yourDOBtextField) {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yy";
yourDOBtextField.text = [dateFormatter stringFromDate:datePicker.date];
[dateFormatter release];
}
}
That's it.
Let's try below codes:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"DD/MM/YY"];
NSDate *date = [dateFormatter dateFromString:#"12/12/2014"];
If (date)
{
NSLog(#"Valid date");
}
I am working with the UIdatepicker. I am fetching the time from the datepicker and showing the date on the label. Once i select the date it is shown on the label and datepicker is hidden away. Now i want that when i call the picker it should show me the selection bar of the datepicker on the date of the label which i selected previously selected on the datepicker.Code i am using is shown below:-
-(void)datepickershown:(UILabel *)time animated:(BOOL)animated
{
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:#"Select the Time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"DONE" otherButtonTitles:Nil, nil];
pickdate = [[UIDatePicker alloc] initWithFrame:[actionsheet bounds]];
pickdate.hidden = NO;
//pickdate.date = [NSDate date];
pickdate.datePickerMode = UIDatePickerModeTime;
[pickdate addTarget:self action:#selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
[actionsheet addSubview:pickdate];
[actionsheet showInView:self.view];
[actionsheet setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickdate.bounds;
pickerRect.origin.y = -90;
pickdate.bounds = pickerRect;
}
- (IBAction)changeDateInLabel:(id)sender
{
NSLog(#"I Am Changing the values");
df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterShortStyle;
}
Image below can describe you more
use setDate:animated of UIDatePicker...
- (void)setDate:(NSDate *)date animated:(BOOL)animated
to convert your label.text to NSDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:#"hh:mm"]; // depends on 12(hh) or 24(HH)
NSDate *yourDate = [dateFormatter dateFromString:label.text];
UIDatePickerView is working fine. It shows up, I can scroll through the dates, but for some reason my selectedOB.text keeps coming up as Todays date! No matter what date I choose, it shows as todays date!
I select this:
NSLog and the label shows up as:
Here is the code:
- (IBAction)selectDOB:(id)sender
{
UIActionSheet *selectBirthMY = [[UIActionSheet alloc] initWithTitle:#"Select Date of Birth" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Select", nil];
[selectBirthMY setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[selectBirthMY showInView:[UIApplication sharedApplication].keyWindow];
[selectBirthMY setFrame:CGRectMake(0, 100, 320, 500)];
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
pickerView.datePickerMode = UIDatePickerModeDate;
[pickerView setMinuteInterval:15];
//Add picker to action sheet
[actionSheet addSubview:pickerView];
//Gets an array af all of the subviews of our actionSheet
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:1] setFrame:CGRectMake(20, 265, 280, 46)];
[[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIDatePicker *DOBPicker = [[UIDatePicker alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY"];
NSDate *pickedDOB = [DOBPicker date];
NSString *DOB = [dateFormatter stringFromDate:pickedDOB];
NSLog (#"This is DOB %#", DOB);
self.selectedDOB.text=DOB;
}
You are using two different instances of UIDatePicker. You need to use the same instance of datePicker to get the selected date
Make a property for datePicker
#property (nonatomic, strong) UIDatePicker *pickerView;
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
if(!self.pickerView){
self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
self.pickerView.datePickerMode = UIDatePickerModeDate;
[self.pickerView setMinuteInterval:15];
//Add picker to action sheet
[actionSheet addSubview:self.pickerView];
}
//Gets an array af all of the subviews of our actionSheet
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:1] setFrame:CGRectMake(20, 265, 280, 46)];
[[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY"];
NSDate *pickedDOB = [self.pickerView date];
NSString *DOB = [dateFormatter stringFromDate:pickedDOB];
NSLog (#"This is DOB %#", DOB);
self.selectedDOB.text=DOB;
}
This is because you are allocating new instance of UIDatePicker in this method
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
So don't create new instance . Use already created datepicker instance .
Hope it helps you.