Abdul Rahman
Abdul Rahman

Reputation: 986

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

}

Upvotes: 0

Views: 650

Answers (2)

Bhumit Muchhadia
Bhumit Muchhadia

Reputation: 128

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.

Upvotes: 1

KrishnaCA
KrishnaCA

Reputation: 5695

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
}

Upvotes: 1

Related Questions