I used another method to remove UIDatePicker.
create a XIB (nib) file and add a UIDatePicker to it, then resize the view so that it matches only the UIDatePicker, create a property (make it strong and non-atomic) in your ViewController (or in any class that you use, and synthesize the course) .
@property (nonatomic, strong) UIView *myDatePickerView; @synthesize myDatePickerView;
then create a loadDatePickerView method
- (void) loadDatePickerView { UINib *nib = [UINib nibWithNibName:kNIBname bundle:[NSBundle mainBundle]]; NSArray *views = [nib instantiateWithOwner:self options:nil]; self.myDatePickerView = [views firstObject]; [myDatePickerView setFrame:CGRectMake(0, 318, 320, 162)]; [self.view addSubview:myDatePickerView]; }
implement UITextFieldDelegate, and in the textFieldDidBeginEditing method call the loadDatePickerView method ,
[self loadDatePickerView]
to create a function that creates a property that is an instance of UIDatePicker
@property (nonatomic,strong) UIDatePicker *myDatePicker;
(synthesized, of course)
now create an IBAction as follows:
-(IBAction)datePickerValueChanged:(UIDatePicker *)sender { myDatePicker = [[myDatePickerView subviews] lastObject];
now connect the IBAction to the collector in the XIB file, so the XIB is now an instance of the UIDatePicker you created in VC, if you want it to disappear, you can add a UITapGestureRecognizer (in the ViewDidLoad) , and the selector will be another IBAction that removes myDatePickerView from its supervisor as follows:
- (void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dropPicker:)]; [self.view addGestureRecognizer:tap]; [self datePickerValueChanged:myDatePicker]; } -(IBAction)dropPicker:(id)sender { [myDatePickerView removeFromSuperview]; }