regarding date picker iOS - ios

I need a datepicker to appear on the tap of a textfield. I have managed to to that. Now I have two problems:
1. It shows date and time both. I need only date.
2. I need the picker to disappear if I tap somewhere in the background.
My code is:
- (void)viewDidLoad
{
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[self.text1 setInputView:datePicker];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)updateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)self.text1.inputView;
self.text1.text = [NSString stringWithFormat:#"%#",picker.date];
}
I have used datePicker.datePickerMode = UIDatePickerModeDate; but still I'm getting the time as well. Can anyone please help?

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSString *yeardate = [dateFormat stringFromDate:[NSDate date]];
//Set this yeardate to your datepicker.
[datePicker setDate:[dateFormat dateFromString:yeardate]];
Hope this helps.

You are assigning the description of date to textField, use NSDateFormatter to convert the date from datePicker.
To dismiss you can add a tapGestureRecognizer to your view or you can add an accessoryInputView to your textField
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapRecognizer = [UITapGestureRecognizer new];
[tapRecognizer addTarget:self action:#selector(didTapView:)];
[self.view addGestureRecognizer:tapRecognizer];
}
- (void)didTapView:(UITapGestureRecognizer *)tapRecognizer{
[self.textField resignFirstResponder];
}
OR
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar =[[UIToolbar alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,44)];
toolbar.barStyle =UIBarStyleBlackOpaque;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelButtonPressed:)];
UIBarButtonItem *flexibleSpace =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doneButtonPressed:)];
[toolbar setItems:#[cancelButton,flexibleSpace, doneButton]];
_activeTextField.inputAccessoryView = toolbar;
}

Related

I have two text field, I required to open datepicker and another one Timepicker in IOS

I have two textfield, if I click first one, I need to open Datepicker and when I click second textfield I need to open timepicker. but the code is availble in viewdidload so I dont know how to call it seperately.
I required like when I tap first textbox it should open datapicker, when I tap the second textbox it should open the Timepicker.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
//[self.navigationItem.rightBarButtonItem.customView setHidden:NO];
dateformater = [[NSDateFormatter alloc]init];
datepictxt.delegate = self;
UITextField *textField = (UITextField*)[self.view viewWithTag:1];
datepicker = [[UIDatePicker alloc]init];
if (textField.tag)
{
datepicker.datePickerMode = UIDatePickerModeDate;
[self.datepictxt setInputView:datepicker];
UIToolbar * toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolbar setTintColor:[UIColor grayColor]];
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showselectiondate:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:space,donebtn, nil]];
[self.datepictxt setInputAccessoryView:toolbar];
}
else
{
datepicker.datePickerMode = UIDatePickerModeTime;
[self.Timepicker setInputView:datepicker];
UIToolbar * toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolbar setTintColor:[UIColor grayColor]];
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showselectiontime:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:space,donebtn, nil]];
[self.Timepicker setInputAccessoryView:toolbar];
}
}
-(void)showselectiontime:(id)response
{
[dateformater setDateFormat:#"hh:mm a"];
self.Timepicker.text= [NSString stringWithFormat:#"%#",[dateformater stringFromDate:datepicker.date]];
[self.Timepicker resignFirstResponder];
}
-(void)showselectiondate:(id)response
{
[dateformater setDateFormat:#"dd/MM/yyyy"];
self.datepictxt.text= [NSString stringWithFormat:#"%#",[dateformater stringFromDate:datepicker.date]];
[self.datepictxt resignFirstResponder];
}
You should use the textField Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
for selecting pickers to open for textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;{
if(textField==self.datepictxt){
// show date picker
}
}
Currently i think your if statement:
if (textField.tag)
is always gonna be true so it shows date, or you need to allocate a new datePicker, instead of using same one for both time and date modes.
This can be done in the following way:
Let's say you have two textFields:
#property (nonatomic) UITextField *datePickerTextField;
#property (nonatomic) UITextField *timePickerTextField;
You can add a UIDatePicker as an inputView in the following way:
- (void)setDatePickerAsInputViewForTextField: (UITextField *)textField {
// creating UIDatePicker with UIDatePickerModeDate
UIDatePicker *datepicker = [[UIDatePicker alloc] init];
datepicker.datePickerMode = UIDatePickerModeDate;
// creating toolbar above to dismiss the UIDatePicker after selecting date
UIToolbar *inputAccessoryToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showSelectedDate:)];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[inputAccessoryToolBar setItems:#[doneButton, spaceButton]];
// Adding UIDatePicker object as an inputView to the UITextField so that it appears in place of Keyboard
[textField setInputView:datepicker];
[textField setInputAccessoryView:inputAccessoryToolBar];
}
-(void)showSelectedDate:(id)picker {
[self.view resignFirstResponder]; // to dismiss UITextField
// remaining code to extract date from picker
}
Similarly,
- (void)setTimePickerAsInputViewForTextField: (UITextField *)textField {
UIDatePicker *timepicker = [[UIDatePicker alloc] init];
timepicker.datePickerMode = UIDatePickerModeTime;
UIToolbar *inputAccessoryToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(showSelectedTime:)];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[inputAccessoryToolBar setItems:#[doneButton, spaceButton]];
[textField setInputView:datepicker];
[textField setInputAccessoryView:inputAccessoryToolBar];
}
-(void)showSelectedTime:(id)picker {
[self.view resignFirstResponder]; // to dismiss UITextField
// remaining code to extract date from picker
}

UIDatePicker does not pop up

In my app, when the user clicks the UITextField, the UIDatePicker should pop up. However, it is not popping up.When I run it, it just shows 'select the date!' on an action sheet and nothing else.
This is my code from my ViewController.m;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(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, 210, 320, 216)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your specific mode
datePicker.backgroundColor = [UIColor whiteColor];
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=UIBarStyleDefault;
[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:[datePicker date]];
}
-(BOOL)closeDatePicker:(id)sender{
[pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
[pd resignFirstResponder];
return YES;
}
-(IBAction)doneButtonClicked{
[self closeDatePicker:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Please I just need help with knowing what I am doing wrong.
Read the description of UIActionSheet in the Xcode docs. It says:
UIActionSheet is not designed to be subclassed, nor should you add
views to its hierarchy.
That last bit is important. You can't add a picker view to an action sheet because you're not supposed to add any views to an action sheet.
BTW, UIActionSheet is was in iOS 8, and we are about to move to iOS 9, so it will have been deprecated for 2 major releases as of this week. Thus you should not be doing new development with it. I suggest you create your own view controller and present it modally rather than trying to use UIActionSheet.
You forgot to show UIActionSheet at the end of -(void)textFieldDidBeginEditing:(UITextField *)textField:
-(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, 210, 320, 216)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your specific mode
datePicker.backgroundColor = [UIColor whiteColor];
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=UIBarStyleDefault;
[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
[pickerViewDate showInView:self.view]; // <---- here, maybe you forgot
}

How to set UIPickerView as inputView to UITextField in iPhone

I am trying to open UIPickerView as inputView to UITextfield.
Here is my code in textFieldDidBeginEditing:
UIPickerView *categoryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43, 320, 480)];
categoryPicker.delegate = self;
categoryPicker.dataSource = self;
[categoryPicker setShowsSelectionIndicator:YES];
txtCategory.inputView = categoryPicker;
UIToolbar *_providerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
_providerToolbar.barStyle = UIBarStyleBlackOpaque;
[_providerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet)];
[barItems addObject:doneBtn];
[_providerToolbar setItems:barItems animated:YES];
txtCategory.inputAccessoryView = _providerToolbar;
It's working fine for first time, but when I am selecting any value from UIPickerView it's hiding and my UITextfield is editable and UIPickerView is not showing at that time. What am I doing wrong?
I'd suggest you to move your code to viewDidLoad:. Because textFieldDidBeginEditing: is called when textField loads the keyboard for it. So It is not the right time to change the inputAccessoryView of the textField.
Use this...
NSDate *now = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM, yyyy"];
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[datePicker setMaximumDate:now];
datePicker.datePickerMode = UIDatePickerModeDate;
txtForDate.text = [dateFormat stringFromDate:datePicker.date];
[datePicker addTarget:self action:#selector(updateTextField:)
forControlEvents:UIControlEventValueChanged];
[txtForDate setInputView:datePicker];
// txtForDate is UITextField

UIDatePicker with UIToolbar

I'm trying to implement UIToolbar on UIDatepicker to dismiss it when touching "Done" button. But when I tap on the button, the datepicker is scrolling and the action button doesn't work.
Here is my code :
datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[datepicker setDatePickerMode:UIDatePickerModeDate];
[datepicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
UIToolbar *toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismiss)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
[datepicker addSubview:toolbar];
My datepicker :
Thank you for your help.
If you use UITextField set
textField.inputAccessoryView = toolbar;
textField.inputView = datepicker;
You have to set inputAccessoryView and InputView to to your UITextField
txtField.inputAccessoryView = toolBar;
txtField.inputView = datePickerView;
remove this line from your code:
[datepicker addSubview:toolbar];
and add dismiss method nd add
txtCurrent.inputAccessoryView = toolbar; in didbeginEditing.
-(void)textFieldDidBeginEditing:(UITextField *)textField {
txtCurrent = textField;
[datePicker setHidden:NO];
txtCurrent.inputAccessoryView = toolbar;
if ([textField.text isEqualToString:#""]) {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = [NSDate date];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
textField.text = [NSString stringWithFormat:#"%#",dateString];
}
[textField setInputView:datePicker];
}
-(void)dismiss{
datePicker.hidden = YES;
[self.view endEditing:YES];
}
Try this [self.view addSubview:toolbar];
picker = [[UIDatePicker alloc] init];
picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
picker.datePickerMode = UIDatePickerModeTime;
[picker addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
//CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
picker.frame = CGRectMake(0.0, self.view.frame.size.height - 250, self.view.frame.size.width, 250);
picker.backgroundColor = [UIColor whiteColor];
toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 294,self.view.frame.size.width,44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(dismiss) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem* doneButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
[self.view addSubview:toolbar];
[self.view addSubview:picker];

Why is this button appearing in all UITextField selections?

I have a UITableViewCell with a text field that's supposed to be for a DatePicker. I need to make a custom button to save the date. But whenever I select any of the UITableViewCell TextFields, the button appears. Why is this?
I'm just trying to "submit" it to the text field so I can save it.
Thanks
- (void)textFieldDidBeginEditing:(UITextField *)dateFieldText
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Save Date"
style:UIBarButtonItemStyleDone
target:self
action:#selector(flipView)];
self.navigationItem.rightBarButtonItem = doneButton;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.dateFieldText setInputView:datePicker];
}
Update
Solved my problem using if else block.
- (void)textFieldDidBeginEditing:(UITextField *)sender
{
sender.delegate = self;
if([sender isEqual:dateFieldText])
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleDone
target:self
action:#selector(saveDate)];
self.navigationItem.rightBarButtonItem = doneButton;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.dateFieldText setInputView:datePicker];
}
else{
UIBarButtonItem *submitButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self
action:#selector(submitList)];
self.navigationItem.rightBarButtonItem = submitButton;
}
}
Thanks for the help.
Are you reusing table view cells? You might consider making a new cell for your date field and giving it a different reuse identifier from your other cells.
You should set the dataPicker to the intended TextField only i.e you should add an if condition to add the datePicker to the required field only, not for all the fields.
- (void)textFieldDidBeginEditing:(UITextField *)sender
{
sender.delegate = self;
if([sender isEqual:dateFieldText])
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleDone
target:self
action:#selector(saveDate)];
self.navigationItem.rightBarButtonItem = doneButton;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.dateFieldText setInputView:datePicker];
}
else{
UIBarButtonItem *submitButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self
action:#selector(submitList)];
self.navigationItem.rightBarButtonItem = submitButton;
}
}

Resources