iPhoneJavaDev
iPhoneJavaDev

Reputation: 825

Custom UITextField not showing

I am trying to create a custom UITextField that shows a date picker instead of a keyboard. I already change the textfields in my storyboard to use DateTextField custom class, however, still the keyboard is being shown instead of the date picker. Note that the textfields are inside a UITableviewCell. Also, i am not sure if this is the correct way of creating the toolbar:

self.datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.superview.bounds.size.width, 44)];



@interface DateTextField : UITextField

@property (strong, nonatomic) UIDatePicker *datePicker;
@property (strong, nonatomic) UIToolbar *datePickerToolbar;

@end


@implementation DateTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.datePicker = [[UIDatePicker alloc] init];
        self.datePicker.datePickerMode = UIDatePickerModeDate;
        self.datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.superview.bounds.size.width, 44)];
        [self.datePickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
        UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(setDatePickerValue:)];
        [self.datePickerToolbar setItems:[[NSArray alloc] initWithObjects:extraSpace, doneButton, nil]];
        self.inputView = self.datePicker;
        self.inputAccessoryView = self.datePickerToolbar;
    }
    return self;
}

@end

Upvotes: 0

Views: 232

Answers (2)

Ravi
Ravi

Reputation: 8309

Instead of using a UITextField, why don't you use a UIButton. It saves a whole lot of time in the future. In this case, the UIButton acts like a UILabel with a tap gesture recognizer.

However, if you need to use UITextField, do this:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([textField isEqual:yourTextField])
    {

        // Present the picker here first 
           ...

        // And then...    
        return NO;
    }

    return YES;
}

Oh and also, do not initialize any UIView subclass properties inside any -initWith... methods. That will trigger a mismatched view lifecycle. Initialize/access these properties inside -viewDidLoad.

Upvotes: 0

Ramaraj T
Ramaraj T

Reputation: 5230

If you add the textfields in storyboard/xib the init method will not be called.

Try with awakeFromNib. Move all the code in the init method to awakeFromNib

    - (void)awakeFromNib
{
    [super awakeFromNib];

   self.datePicker = [[UIDatePicker alloc] init];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.superview.bounds.size.width, 44)];
    [self.datePickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
    UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(setDatePickerValue:)];
    [self.datePickerToolbar setItems:[[NSArray alloc] initWithObjects:extraSpace, doneButton, nil]];
    self.inputView = self.datePicker;
    self.inputAccessoryView = self.datePickerToolbar;

}

Upvotes: 2

Related Questions