我有一个带有文本字段的 UITableViewCell,它应该用于 DatePicker。我需要制作一个自定义按钮来保存日期。但是,每当我选择任何 UITableViewCell TextFields 时,都会出现该按钮。为什么是这样?
我只是想将它“提交”到文本字段,以便我可以保存它。
谢谢
- (void)textFieldDidBeginEditing:(UITextField *)dateFieldText
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:@"Save Date"
style:UIBarButtonItemStyleDone
target:self
action:@selector(flipView)];
self.navigationItem.rightBarButtonItem = doneButton;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.dateFieldText setInputView:datePicker];
}
更新
使用 if else 块解决了我的问题。
- (void)textFieldDidBeginEditing:(UITextField *)sender
{
sender.delegate = self;
if([sender isEqual:dateFieldText])
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:@"Save"
style:UIBarButtonItemStyleDone
target:self
action:@selector(saveDate)];
self.navigationItem.rightBarButtonItem = doneButton;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.dateFieldText setInputView:datePicker];
}
else{
UIBarButtonItem *submitButton = [[UIBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStyleDone
target:self
action:@selector(submitList)];
self.navigationItem.rightBarButtonItem = submitButton;
}
}
谢谢您的帮助。