How to hide/show date picker on button click? - ios

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;
}

Related

UIPickerView and UIDatePicker are not responding to interaction

I have a UITextField in a UITableView. I am trying to have a UIPickerView or a UIDatePicker as its input view. Both options are appearing and in case of UIPickerView, it is loading correct data. The problem is that it is not scrollable and user cannot interact with it, choose and scroll between the various options.
Userinteraction is set to yes, delegates are correct and everything is in place.
This is the code:
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
UIDatePicker *datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[datepicker setDatePickerMode:UIDatePickerModeDate];
textField.inputView = datepicker;
return YES;
}
Why are you set the inputView in this method?
Do this in viewDidLoad. This is a configuration,
doesn't make sense set a particular inputView each time the user focus on that textField:
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(editTextField:) forControlEvents:UIControlEventValueChanged];
[textField setInputView:datePicker];
}
and then:
- (void)editTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)textField.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
textField.text = [NSString stringWithFormat:#"%#",dateString];
}

ViewController goes black when pushing it in UINavigationcontroller stack

I have a UInavigationController stack. In the RootViewController i have button . When i click the button a new viewcontroller is pushed and UIDatePicker should be shown . the problem is when navigating to view controller the date picker is shown for 1 sec and then suddenly disappeared and the whole screen goes Black. Here is the code
//rootviewcontroller
- (IBAction)dateAction:(id)sender
{
self.another = [[AnotherViewController alloc]initWithButtonWithTag:self.dateButton.tag andDelegate:self];
[self.navigationController pushViewController:self.another animated:YES];
self.another.navigationItem.title = #"DateName";
}
//second viewcontroller
- (void)viewDidload
{
self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
[self.pickerView addTarget:self action:#selector(datePickerDidChangeDate:) forControlEvents:UIControlEventValueChanged];
self.pickerView.datePickerMode = UIDatePickerModeDate;
self.pickerView.hidden = NO;
self.pickerView.date = [NSDate date];
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateStyle = NSDateFormatterLongStyle;
[self.view addSubview:self.pickerView];
}
A help would be appreciated
try this codeā€¦
first view controller
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *temp=[NSArray arrayWithObjects:#"button1" ,#"button2" ,#"button3",nil];
for (int i=0; i<=2; i++)
{
_likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[_likeBtn setFrame:CGRectMake(20+(i*80), 220, 102, 20 )];
[_likeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_likeBtn setTitle:[temp objectAtIndex:i] forState:UIControlStateNormal];
[_likeBtn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_likeBtn setTag:i];
[self.view addSubview:_likeBtn];
}
// Do any additional setup after loading the view from its nib.
}
- (IBAction)action:(id)sender
{
UIButton *button=(UIButton *)sender;
NSLog(#"%d",button.tag);
secViewController *obj = [[secViewController alloc]init];
obj.tagValue=button.tag;
[self.navigationController pushViewController:obj animated:YES];
obj.navigationItem.title = #"DateName";
}
second view .h file
#property(nonatomic,assign) NSInteger *tagValue;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger *tag=self.tagValue;
if (tag==0)
{
//tableView
NSLog(#"tableview");
}
else
{
//datepicker
NSLog(#"picker");
self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
[self.pickerView addTarget:self action:#selector(datePickerDidChangeDate:) forControlEvents:UIControlEventValueChanged];
self.pickerView.datePickerMode = UIDatePickerModeDate;
self.pickerView.hidden = NO;
self.pickerView.date = [NSDate date];
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateStyle = NSDateFormatterLongStyle;
[self.view addSubview:self.pickerView];
}
// Do any additional setup after loading the view from its nib.
}
-(void)datePickerDidChangeDate:(id)sender
{
NSLog(#"%#",self.pickerView.date);
}
let me know statisfied or not...

Cannot hide UIDatePicker

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.

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