Replacing UITextField Input with UIDatePicker

I have a table view controller with (among others) a cell that should represent a date. I followed this post β€œ How to make a modal date picker that covers only half the screen? ” And I have work with one big exception - I can’t get the collector to disappear!

I tried to register for the UIControlEventTouchUpOutside event, but it seems that this is not generated by the collector (or at least in the mode in which I use it).

How can I find out that the user has completed the date selection?

Also, is there a way to disconnect a user from direct input in a UITextField? I want to get them to use the collector. I saw this post Disable blinking cursor in UITextField? "but is there any other way?

Reagards,

- John

+6
source share
2 answers

Try this code .. here I put the datepicker in uitextfield .. it will have a button in the upper right navigation bar .. so by clicking the done button I can remove the datepicker user .. another best method is that you put the toolbar above the date by clicking done. Try this, it will work. When changing the datepicker, you can fill in the text box. Hope this helps.

see this stackoverflow fooobar.com/questions/179153 / ... link, it will have a datepicker button with a finished button in the form of a toolbar above the keyboard.

#pragma mark - #pragma mark - TextField Delegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return TRUE; } - (void) textFieldDidBeginEditing:(UITextField *)textField { itsRightBarButton.title = @"Done"; itsRightBarButton.style = UIBarButtonItemStyleDone; itsRightBarButton.target = self; itsRightBarButton.action = @selector(doneAction:); if ([textField isEqual:itsIncidentDateTextField]) { itsDatePicker = [[[UIDatePicker alloc] init] autorelease]; itsDatePicker.datePickerMode = UIDatePickerModeDate; [itsDatePicker addTarget:self action:@selector(incidentDateValueChanged:) forControlEvents:UIControlEventValueChanged]; //datePicker.tag = indexPath.row; textField.inputView = itsDatePicker; } } - (IBAction) incidentDateValueChanged:(id)sender{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MMM d, yyyy"]; itsIncidentDateTextField.text = [dateFormatter stringFromDate:[itsDatePicker date]]; [dateFormatter release]; } 
+12
source

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 you can do whatever you want with the DatePicker } 

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]; } 
-2
source

Source: https://habr.com/ru/post/918250/


All Articles