gpt4 book ai didi

ios - 我有两个文本字段,我需要在 IOS 中打开日期选择器和另一个时间选择器

转载 作者:行者123 更新时间:2023-11-28 21:03:27 30 4
gpt4 key购买 nike

我有两个文本框,如果我点击第一个,我需要打开日期选择器,当我点击第二个文本框时,我需要打开时间选择器。但代码在 viewdidload 中可用,所以我不知道如何单独调用它。

我要求当我点击第一个文本框时它应该打开数据选择器,当我点击第二个文本框时它应该打开时间选择器。

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

}

最佳答案

你应该使用 textField Delegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;

用于选择选择器以打开文本字段

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;{
if(textField==self.datepictxt){

// show date picker

}
}

目前我认为你的 if 语句:

 if (textField.tag)

始终为真,因此它显示日期,或者您需要分配一个新的 datePicker,而不是对时间和日期模式使用相同的 datePicker。

关于ios - 我有两个文本字段,我需要在 IOS 中打开日期选择器和另一个时间选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47041765/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com