Cannot hide UIDatePicker - ios

For my app, I've attempted to make a UIDatePicker appear when the textfield called time is clicked.
In my viewDidLoad, I initialize it like this:
- (void)viewDidLoad {
time.delegate = self;
[time setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
datepicker = [[UIDatePicker alloc] init];
datepicker.datePickerMode = UIDatePickerModeCountDownTimer;
dateformatter= [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"HH:mm:ss"];
}
Then, in the textFieldShouldBeginEditing method, I have this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == time){
button.hidden = NO;
datepicker.hidden = NO;
}
return NO;
}
However, my UIDatePicker still stays hidden, and I am sure the outlets are connected

remove this line from your code and check ,if your picker is placed in Xib.
datepicker = [[UIDatePicker alloc] init];

In textFieldShouldBeginEditing you need to set frame according to your need and than you have to add date picker to your view. for example
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == _time){
CGRect frame = datepicker.frame;
frame.origin.x = 100(for eg,set according to you);
frame.origin.y = 100(for eg,set according to you);
datepicker.frame = frame;
[self.view addSubview:datepicker];
button.hidden = NO;
datepicker.hidden = NO;
}
return NO;
}
remove after you done with this.

Related

How to hide/show date picker on button click?

I have a UIDatePicker and I want to hide and show on UIButton Click .
For this I am using BOOL variable isTouchFirst.but it hide and show on second event of button.
Please resolve this problem.And also check first it work on double click event.
if(isTouchFirst){
isTouchFirst=NO;
self. DatePicker.hidden=NO;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
_DatePicker.datePickerMode = UIDatePickerModeDate;
formatedDate=[[NSString alloc]init];
formatedDate = [dateFormatter stringFromDate:self.DatePicker.date];
self.SelectedDate.text =formatedDate;
[DatePicker setMinimumDate: [NSDate date]];
_goDateForcustmoeDetail=formatedDate;
}
else {
isTouchFirst=YES;
self. DatePicker.hidden=YES;
}
BOOL datePickerHidden;
- (void)viewDidLoad
{
[super viewDidLoad];
datePickerHidden = YES;
// _datePicker = [UIDatePicker alloc...
[_datePicker setHidden: datePickerHidden];
// UIButton *dateHiddenSwitchBtn = [UIButton alloc...
dateHiddenSwitchBtn addTarget:self action:#selector(switchDatePickerHidden) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dateHiddenSwitchBtn];
}
- (void)switchDatePickerHidden
{
if (datePickerHidden) datePickerHidden = NO;
else datePickerHidden = YES;
[_datePicker setHidden: datePickerHidden];
}
Make it hidden = YES by default
- (void)buttonTapped:(UIButton *)sender {
sender.selected = !sender.selected;
datePicker.hidden = !sender.selected;
}

UIDatePicker to set UITextfield text

I have a UITextField and a UIDatePicker which I want to use to set the Birthday of the user.
I use this code in the viewDidLoad:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
birtdayTextField.inputView = datePicker;
However I have 2 problems. I've placed a UIDatePicker in my view in the storyboard. But it still opens a new datepicker even though I've connected the *datepicker to the UIDatePicker.
And also it doesn't add the info from the datepicker to the textfield.
Input view doesn't work so. You may create lazy UIDatePicker:
- (UIDatePicker *)bDatePicker {
if (!_bDatePicker) {
_bDatePicker = [UIDatePicker new];
_bDatePicker.datePickerMode = UIDatePickerModeDate;
[_bDatePicker addTarget:self action:#selector(datePickerDidChangeDate:) forControlEvents:UIControlEventValueChanged];
}
return _bDatePicker;
}
Then set it like following:
self.birtdayTextField.inputView = self.bDatePicker;
And this is the callback:
- (void)datePickerDidChangeDate:(UIDatePicker *)sender {
self.birtdayTextField.text = [self.bDateFormatter stringFromDate:sender.date];
}
Possible formatter:
- (NSDateFormatter *)bDateFormatter {
if (!_bDateFormatter) {
_bDateFormatter = [NSDateFormatter new];
_bDateFormatter.dateFormat = #"dd.MM.yyyy";
}
return _bDateFormatter;
}
To prevent pasting wrong data to that text field you may use it's delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return textField != self.birtdayTextField;
}

UIPIckerView showing strange behaviour

I'm new to iOS development and I have an assignment where I need to implement a a UITextfield which upon a tap, brings a country selector UIPickerView.
I went through some online tutorials and got it working somehow. But it shows some strange behaviour which I can't seem to figure out.
Some strange black bars are appearing, with the picker content visibly duplicated each time I scroll.
I believe I've made a mistake in my code, forgive my lack of knowledge but can't seem to figure out what is wrong in the code.
Could you please tell me what has gone wrong?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:self.countryTextField]) {
countryPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
countryPicker.showsSelectionIndicator = YES;
self.countryPicker.delegate = self;
self.countryPicker.dataSource = self;
// [self.countryPicker reloadAllComponents];
countryPicker.showsSelectionIndicator = YES;
// [countryPicker init:self];
textField.inputView = countryPicker;
}
return YES;
}
- (UIView * ) pickerView: (UIPickerView * ) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (UIView * ) view {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.text = [NSString stringWithFormat:#"%#", [[self.countries objectAtIndex:row] objectForKey:#"name"]];
[label setTextAlignment:UITextAlignmentLeft];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:15]];
return label;
}
Might be initialising every time , instead of initialising at every time , Check whether the UIPicker is already initialised
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:self.countryTextField]) {
if(! countryPicker){
countryPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
countryPicker.showsSelectionIndicator = YES;
self.countryPicker.delegate = self;
self.countryPicker.dataSource = self;
countryPicker.showsSelectionIndicator = YES;
}
textField.inputView = countryPicker;
}
return YES;
}
It seems there are few things need to be changed in your code
1.Don't allocate memory to UIPickerView every time your text field get edited -textFieldShouldBeginEditing
if(self.countryPicker == nil)
{
self.countryPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
self.countryPicker.showsSelectionIndicator = YES;
self.countryPicker.delegate = self;
self.countryPicker.dataSource = self;
self.countryPicker.showsSelectionIndicator = YES;
}
2.Either use self.countrypicker -> class property or countrypicker don't use them interchangeably.
Well I finally found the issue. It was a silly mistake of defining a wrong number of components in numberOfComponentsInPickerView

UITextField inputView - Can the selected view reference the textfield?

If I have a set up like so:
- (void)textFieldDidBeginEditing:(UITextField* )textField {
if (textField.tag == 603) {
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(showDate:) forControlEvents:UIControlEventValueChanged];
textField.inputView = datePicker;
}
}
in the setDate method, can I reference which textField the UIPickerDate is the inputView of? That sounds confusing but like something like this:
- (void) showDate: (UIDatePicker*) myDatePicker {
NSDate* selected = [myDatePicker date];
NSString* date = [selected description];
myTextField = myDatePicker.view //I know this is not a property
}
kind of in the same sense that a UITapGestureRecognizer knows what view is calling it...
Explanation for Woz: I could but it would make the app less dynamic. This is a big form for the installation of medical devices. To create the form I store all the element properties in a dictionary (label, type (text, checkbox, segmented control), form section, etc.) and then each dict in an array in the proper order. I programatically build everything while in the array loop. I guess I could subclass UITextField...but you got to admit it would be nice to know which textfield initiated the inputView
I just subclassed UITextField and got it to work:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.textColor = [colorManager setColor:66.0 :66.0 :66.0];
self.font = [UIFont fontWithName:#"Helvetica" size:18.0];
self.backgroundColor = [UIColor whiteColor];
self.borderStyle = UITextBorderStyleRoundedRect;
self.returnKeyType = UIReturnKeyDone;
self.userInteractionEnabled = YES;
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(setDate:) forControlEvents:UIControlEventValueChanged];
self.inputView = datePicker;
}
return self;
}
- (void) setDate : (UIDatePicker*) myDatePicker {
NSDateFormatter* myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString* dateString = [myDateFormatter stringFromDate:myDatePicker.date];
self.text = dateString;
}
The UITextField that triggered the UIDatePicker would be the current firstResponder. You can use this info to identify the textfield that is associated with the currently active UIDatePicker.
Add a tag to UIDatePicker and use the tag to find textField from view.
- (void)textFieldDidBeginEditing:(UITextField* )textField {
if (textField.tag == 603) {
datePicker = [[UIDatePicker alloc]init];
[datePicker setTag:textField.tag];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(showDate:) forControlEvents:UIControlEventValueChanged];
textField.inputView = datePicker;
}
}
- (void) showDate: (UIDatePicker*) myDatePicker {
NSDate* selected = [myDatePicker date];
NSString* date = [selected description];
UITextField *textField = (UITextField *)[self.view viewWithTag:myDatePicker.tag];
}

How to get value from two textFields?

I have two textFields and I have replaced default tap behavior from keyboard to DatePicker
-(void)updateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)_editStartDate.inputView;
_editStartDate.text = [NSString stringWithFormat:#"%#",picker.date];
}
- (void)viewDidLoad
{
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date];
[datePicker addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[_editStartDate setInputView:datePicker];
...
}
This code create datepicker instead of keyboard and update textField with name editStartDate, but I have 2nd textField with name editEndDate and I don't know how to get value in it too.
Do you have any ideas ?
Try this one:
-(void)updateTextField:(id)sender
{
if([_editStartDate isFirstResponder]){
UIDatePicker *picker = (UIDatePicker*)_editStartDate.inputView;
_editStartDate.text = [NSString stringWithFormat:#"%#",picker.date];
}
if([_editEndDate isFirstResponder]){
UIDatePicker *picker = (UIDatePicker*)_editEndDate.inputView;
_editEndDate.text = [NSString stringWithFormat:#"%#",picker.date];
}
}

Resources