我有一个UITableViewCell,其文本字段应该是DatePicker。我需要制作一个自定义按钮来保存日期。但每当我选择任何UITableViewCell TextField时,都会出现按钮。这是为什么?
我只是想把它“提交”到文本字段,这样我就可以保存它。
由于
- (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;
}
}
感谢您的帮助。
答案 0 :(得分:0)
您是否在重复使用表格视图?您可以考虑为日期字段创建一个新单元格,并为其提供与其他单元格不同的重用标识符。
答案 1 :(得分:0)
你应该将datePicker设置为预期的文本字段i.f你应该添加一个if条件,只将datePicker添加到所需的字段,而不是所有的字段。
答案 2 :(得分:-1)
- (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;
}
}