iOS:点击UITextView时UIDatePicker没有显示

时间:2013-12-23 05:26:41

标签: ios objective-c uitableview

我的UITextField上有一个UITableViewCell,供用户选择时间。我想按UIDatePickerUITextField,但我的下面的代码无效。点击文本字段仍然显示默认键盘。

EditQuotaCell.h

@interface EditQuotaCell : UITableViewCell

@property (nonatomic, weak) id delegate;

@property (nonatomic, strong) IBOutlet UILabel *lblTimeRange;

@property (nonatomic, strong) IBOutlet UITextField *txtTimeStart;
@property (nonatomic, strong) IBOutlet UITextField *txtTimeEnd;

@property (nonatomic, strong) UIDatePicker *timePicker;

@end

EditQuotaCell.m

@implementation EditQuotaCell

@synthesize lblTimeRange;
@synthesize txtTimeStart, txtTimeEnd;

@synthesize timePicker;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

    }
    return self;
}

- (id) initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
    {
        self.timePicker = [[UIDatePicker alloc] init];

        self.timePicker.datePickerMode = UIDatePickerModeDate;
        [self.timePicker setDate:[NSDate date]];
        [self.timePicker setMinuteInterval:30];

        self.txtTimeStart.inputView = self.timePicker;
    }

    return self;
}

......

3 个答案:

答案 0 :(得分:0)

请将您的选择器视图设置为viewdidload

中文本字段的输入视图
  

txtTimeStart.inputView = timePicker;

答案 1 :(得分:0)

您可以尝试使用viewDidLoad:方法

尝试在inputView方法中为UITextField设置viewDidLoad:

- (void)viewDidLoad
{
    self.timePicker = [[UIDatePicker alloc] init];

    self.timePicker.datePickerMode = UIDatePickerModeDate;
    [self.timePicker setDate:[NSDate date]];
    [self.timePicker setMinuteInterval:30];

    self.txtTimeStart.inputView = self.timePicker;
}

答案 2 :(得分:0)

通过移动inputView中的awakeFromNib设置来解决此问题。

- (void) awakeFromNib
{
    self.txtTimeStart.inputView = self.timePicker;
}