Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use the <code>UITextField</code> input view property. Just alloc init a regular <code>UIDatePicker</code> in code and assign it to the textfield's inputView. Doing this, the <code>UIPickerView</code> will be presented instead of the keyboard. If you need any code feel free to ask :)</p> <p>EDIT: code</p> <p>You fist hook up your <code>textField</code> with IB</p> <pre><code>@interface myViewController &lt;UITextFieldDelegate&gt; @property (nonatomic,weak) IBOutlet UITextField *myTextField; @end </code></pre> <p>In the implementation </p> <pre><code>UIDatePicker *datePicker = [[UIDatePicker alloc]init]; [datePicker setDate:[NSDate date]]; [datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged]; [self.myTextField setInputView:datePicker]; </code></pre> <p>That will bring up a <code>UIDatePicker</code> when you tap on the <code>textField</code>. Note the action that I added to the datePicker.</p> <pre><code>[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged]; </code></pre> <p>To update the textField each time that the user scrolls in the picker, you just need to implement the <code>updateTextField</code> method somewhere in the <code>viewController</code></p> <pre><code>-(void)updateTextField:(id)sender { UIDatePicker *picker = (UIDatePicker*)self.myTextField.inputView; self.myTextField.text = [NSString stringWithFormat:@"%@",picker.date]; } </code></pre> <p>With this last method, every time the user rotates the picker, the textfield will update!</p> <ul> <li>You could use a <code>NSDateFormatter</code> to improve the output of the string. </li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload