简体   繁体   中英

iOS: UIDatePicker not showing when tap on UITextView

I have a UITextField on my UITableViewCell for the user to select a time. I want to display a UIDatePicker on press the UITextField but my below code is not working. Tapping the textfield still shows me the default keyboard.

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;
}

......

please set your picker view as input view for your textfield in viewdidload

txtTimeStart.inputView = timePicker;

May be you should try with viewDidLoad: method

try to set the inputView for your UITextField in viewDidLoad: method

- (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;
}

solved it by moving the setting the inputView in awakeFromNib .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM