A simple way to add a date picker input in iOS


Warning: count(): Parameter must be an array or an object that implements Countable in /home/customer/www/logansease.com/public_html/iparty/wp-content/themes/grizzly-theme/base/shortcodes/fix.php on line 36

(in ViewDidLoad)

//setup the datePicker input UIDatePicker * datePicker = [[UIDatePicker alloc] init] ; datePicker.datePickerMode = UIDatePickerModeDate; datePicker.date = [NSDate date]; self.expirationDateText.inputView = datePicker; //retain a copy of the date picker if needed self.expirationDatePicker = datePicker; //add a call back to update the text field when the date picker changes [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged]; //add a recogizer to hide the keyboard when tapping the screen [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyboard:)]];

and if you want to add a toolbar with a done button… just add this!

self.expirationDateText.inputAccessoryView = [self getKeyboardAccessoryWithTitle:@“Done" andSelector:@selector(hideKeyboard)]; -(UIToolbar*)getKeyboardAccessoryWithTitle:(NSString*)title andSelector:(SEL)selector { UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; numberToolbar.barStyle = UIBarStyleBlackTranslucent; numberToolbar.items = [NSArray arrayWithObjects: //[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)], [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStyleDone target:self action:selector], nil]; [numberToolbar sizeToFit]; return numberToolbar; }

Comments

  Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

-->