0

我有一个带有文本字段的 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;
        }  
    }

谢谢您的帮助。

4

3 回答 3

0

您是否在重复使用表格视图单元格?您可能会考虑为您的日期字段创建一个新单元格,并为其提供与其他单元格不同的重用标识符。

于 2013-03-04T01:30:42.877 回答
0

您应该仅将 dataPicker 设置为预期的 TextField,即您应该添加一个 if 条件以将 datePicker 仅添加到所需字段,而不是所有字段。

于 2013-03-04T02:37:30.903 回答
-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;
        }  
    }
于 2013-03-04T02:51:40.113 回答