简体   繁体   English

对两个TextField使用相同的UIDatePicker

[英]Using Same UIDatePicker For Two TextFields

i am having two textfields,fromdate and todate.how to get different dates from the same datepicker.i tried something which ended up in getting the same date in both the textfields when the date is changed. 我有两个文本域,fromdate和todate.how从同一个datepicker获取不同的日期。我尝试了一些东西,最终在日期更改时在两个文本字段中获得相同的日期。

-(IBAction)dateValueChanged:(id)sender
{
        UIDatePicker *picker = (UIDatePicker *)sender;
        NSDate *dateSelected1 = [picker date];
        NSDate *dateSelected2 = [picker date];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy"];
        self.fromTextField.text = [dateFormatter stringFromDate:dateSelected1];
       self.toTextField.text = [dateFormatter stringFromDate:dateSelected2];
}

A more elegant way available of implementing this without UITextFieldDelegate and textFieldDidBeginEditing: 在没有UITextFieldDelegatetextFieldDidBeginEditing:情况下实现此更优雅的方法textFieldDidBeginEditing:

Just with checking firstResponder 只需检查firstResponder

- (IBAction)dateValueChanged:(id)sender
{
    NSDate *dateSelected = [picker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];

    if ([firstTextField isFirstResponder])
    {
      firstTextField.text = [dateFormatter stringFromDate:dateSelected];
    }
    else if ([self.endTextField isFirstResponder])
    {
        secondTextField.text = [dateFormatter stringFromDate:dateSelected];
    }
}

You could implement the UITextFieldDelegate Protocol for both your TextFields textFieldDidBeginEditing: Method along with setting the tag property for each TextField so they are easily identifiable... 你可以为TextFields textFieldDidBeginEditing: Method实现UITextFieldDelegate协议,同时为每个TextField设置tag属性,以便它们易于识别......

Then when textFieldDidBeginEditing: is called, you could read the tag of the textfield that has begun editing and set that as a global value so you can find out what textfield the date picker should change.. example: 然后,当调用textFieldDidBeginEditing:时,您可以读取已开始编辑的文本字段的标记并将其设置为全局值,以便您可以找出日期选择器应更改的文本字段。示例:

- (void)textFieldDidBeginEditing:(UITextField *)textField {

     someGlobalNSInteger = textField.tag; //the current text field tag is now globally set

}

-(IBAction)dateValueChanged:(id)sender {
    UIDatePicker *picker = (UIDatePicker *)sender;
    NSDate *dateSelected1 = [picker date];
    //NSDate *dateSelected1 = [picker date];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];

    //self.fromTextField.text = [dateFormatter stringFromDate:dateSelected1];
    //self.toTextField.text = [dateFormatter stringFromDate:dateSelected2];

    UITextField *activeTextField = (UITextField*)[self viewWithTag:someGlobalNSInteger]; //gets text field what is currently being edited
    [activeTextField setText:[dateFormatter stringFromDate:dateSelected1]];

}

However if you're not allowing the text field to be editable (so it can't bring the keyboard up) you may need some other way of figuring out what textField should get the updated date but i'll leave that to you. 但是,如果您不允许文本字段可编辑(因此无法启动键盘),您可能需要一些其他方法来确定textField应该获取更新日期,但我会留给您。

Hope it helps 希望能帮助到你

The best way to achieve this is to use inputview property available in the UITextField. 实现此目的的最佳方法是使用UITextField中提供的inputview属性。 Initialize your date picker once either using Outlet or programming & assign it as inputView in the textfield's delegate method "textFieldDidBeginEditing:". 使用Outlet或编程初始化日期选择器并在textfield的委托方法“textFieldDidBeginEditing:”中将其指定为inputView。 For assigning date you may use your own business logic before actually assigning the inputview to the textfield. 对于分配日期,您可以在将inputview实际分配给文本字段之前使用自己的业务逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM