UITextField不会将datePicker.date显示为inputview

时间:2014-04-11 02:12:09

标签: ios objective-c uitableview uitextfield uidatepicker

当您点按dateTextField上的tableViewCell时,会显示datePicker 您从datePicker选择日期 但dateTextField.text不显示datePicker.date

我该如何解决?

我有以下代码。

tableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellIdentifier = @"Cell";    
    tableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if( cell == nil )
    {
        cell = [[tableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDateFormatter* outputFormatter = [[NSDateFormatter alloc]init];
    [outputFormatter setDateFormat:@"HH:mm"];
    NSString* dateString = [outputFormatter stringFromDate:cell.datePicker.date];
    cell.dateTextField.text = dateString;
    [cell.contentView addSubview:self.dateTextField];

    return cell;
}

tableViewCell.m

@property (nonatomic, retain) UITextField* dateTextField;
@property (nonatomic, retain) UIDatePicker* datePicker;
@property (nonatomic, retain) UIToolbar* toolBar;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self.datePicker = [[UIDatePicker alloc]init];
    self.datePicker.datePickerMode = UIDatePickerModeTime;
    self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, -40, 320, 40)];
    self.toolBar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(textFieldShouldReturn:)];
    NSArray* barItems = [NSArray arrayWithObjects:doneButton, nil];
    [self.toolBar setItems:barItems animated:YES];

    self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 5, 100, 25)];
    self.dateTextField.delegate = self;
    self.dateTextField.inputView = self.datePicker;
    self.dateTextField.inputAccessoryView = self.toolBar;
    [self.contentView addSubview:self.dateTextField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.dateTextField resignFirstResponder];
    return YES;
}

谢谢。

2 个答案:

答案 0 :(得分:0)

您的代码不是很清楚(似乎缺少其他重要部分),但我相信这样做的正确方法是在用户按下完成后从日期选择器中获取日期(由IBAction通知),然后自定义格式化它并将textField的字符串设置为您的格式字符串。祝你好运!

答案 1 :(得分:0)

我通过添加setText方法解决了这个问题 固定代码:

@property (nonatomic, retain) UITextField* dateTextField;
@property (nonatomic, retain) UIDatePicker* datePicker;
@property (nonatomic, retain) UIToolbar* toolBar;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self.datePicker = [[UIDatePicker alloc]init];
    self.datePicker.datePickerMode = UIDatePickerModeTime;
    self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, -40, 320, 40)];
    self.toolBar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(textFieldShouldReturn:)];
    NSArray* barItems = [NSArray arrayWithObjects:doneButton, nil];
    [self.toolBar setItems:barItems animated:YES];

    self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 5, 100, 25)];
    self.dateTextField.delegate = self;
    self.dateTextField.inputView = self.datePicker;
    self.dateTextField.inputAccessoryView = self.toolBar;
    [self.contentView addSubview:self.dateTextField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSDateFormatter* outputFormatter = [[NSDateFormatter alloc]init];
    [outputFormatter setDateFormat:@"HH:mm"];
    NSString* dateString = [outputFormatter stringFromDate:self.datePicker.date];
    [self.dateTextField setText:dateString];
    [self.dateTextField resignFirstResponder];
    return YES;
}