我有两个文本字段,如果我单击第一个,我需要打开Datepicker,当我单击第二个文本字段时,我需要开放timepicker。但是代码在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,而不是对时间和日期模式都使用同一个。
这可以通过以下方式完成:
假设您有两个文本字段:
@property (nonatomic) UITextField *datePickerTextField;
@property (nonatomic) UITextField *timePickerTextField;
您可以通过以下方式将UIDatePicker
添加为inputView
:
- (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
}
类似地,
- (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
}