0

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

}
VyTcdc
  • 986
  • 10
  • 32
  • How is this acting currently? What exactly happens when you click on the relevant text fields? – Casey West Oct 31 '17 at 18:00
  • I am getting only Datepickker not timepicker – VyTcdc Oct 31 '17 at 18:07
  • `UITextField *textField = (UITextField*)[self.view viewWithTag:1];` This line is finding the textfield of tag 1 and you are checking the same Tag which will always be 1. Therefor, if block is executed always. – dispatchMain Oct 31 '17 at 18:34
  • so how to resolve plse suggest – VyTcdc Oct 31 '17 at 18:37
  • From what I could understand, you should implement `textFieldDidBeginEditing:` from `UITextField` and keep your `if-else` block in that method. Compare tag or textField object itself to decide picker mode and then display the picker. – dispatchMain Oct 31 '17 at 18:42
  • Ok I will check – VyTcdc Oct 31 '17 at 18:46
  • @VyTcdc, I added explanation for what is happening in the code and divided it to functions for your need. Let me know if you need any help – KrishnaCA Oct 31 '17 at 19:00

2 Answers2

1

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
}
KrishnaCA
  • 5,615
  • 1
  • 21
  • 31
1

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.

  • I tried textFieldShouldBeginEditing as you said It is working, I made a mistake, I did not add this in viewdidload Timepicker.delegate = self; so only getting datepicker after that fiqure out the problem and resolve it, thanks. – VyTcdc Nov 01 '17 at 15:04